Amongst all your assets as a programmer, your log files are paramount... Logging is simply keeping logs. This could be a record of an event. These events can be within softwares, operating systems or even messages between users of a particular software.
It is more than necessary and useful at the same time to keep logs of every event within your software. It enhances troubleshooting.
Complications can arise from any point, and for a start - you need to at least convince yourself first, that there really is a problem before trying to diagnose. Ever had a time where the compiler started complaining about your code "for no reason"?
Let me select a few benefits logging can offer you, and let these stand for many more. Logging events are incredibly valuable because it gives you a comprehensive review stream of every cause of action of your system/application. Whether a process or thread has started, stopped, broken or even, why the compiler was right for being grumpy.
Debugging without context can be very depressing, but with logs, you can bet there is hope. Brian Kernighan said:
Debugging is twice as hard as writing the code in the first place.
I think logging must be our safe haven. Times are hard, log to the Rescue!
This is a very basic but useful example I created with Python.
def __init__(self, class_name):
self.logger = logging.getLogger(class_name)
self.file_handler = logging.FileHandler('system.log')
formatter = logging.Formatter('%(asctime)s - %(name)s - %(levelname)s - %(message)s')
def debug(self, message):
def info(self, message):
def warn(self, message):
def error(self, message):
def critical(self, message):
To use the Logger Class, you must first instantiate it (obviously), with a unique identification for that class or file. This is for easy identification of where to look into.
For this example, I used
from logger import Logger if __name__ == "__main__": log = Logger("Driver") log.debug("This is a debug message") log.info("This is an info message") log.warn("This is a warning message") log.error("This is an error message") log.critical("This is a Critical message")
system.log - Contains all logs for rescue purposes ;)
2018-02-01 17:12:44,329 - Driver - INFO - This is an info message
2018-02-01 17:12:44,329 - Driver - WARNING - This is a warning message
2018-02-01 17:12:44,329 - Driver - ERROR - This is an error message
2018-02-01 17:12:44,329 - Driver - CRITICAL - This is a Critical message
The logging functions are named after the level or severity of the events they are used to track. You will observe that the output isn't complete. Noticed the
debug message is missing? This is because of the severity level I initialized. The simple fix will be to change the argument of
setLevel() method from
logging.DEBUG and done.
2018-02-01 17:12:50,330 - Driver - DEBUG - This is an debug message
2018-02-01 17:12:50,330 - Driver - INFO - This is an info message
2018-02-01 17:12:50,330 - Driver - WARNING - This is a warning message
2018-02-01 17:12:50,330 - Driver - ERROR - This is an error message
2018-02-01 17:12:50,330 - Driver - CRITICAL - This is a Critical message
You can use it accordingly or as you need it. Logging is pretty much supported in most programming languages just ask Google.