Common Logger sturucture for any project

Monitoring, troubleshooting and debugging is most imporatnt part of software developer's life.

Logging makes this a much easier and smoother process.

Common Format of logger

Format of each line in the log file. Each line must start with "~# " (tilde hash space)

									

~# datetime|application|file.method|trace id|severity|source ip| any message etc

Datetime - timestamp of event

Application/file.method : Name of application. That is dependent on project. If project has multiple micro service or multiple application like you have 2 application “student” and “salaried”, then you can use “admin” and “users" as a “application name” then you need to include class name and method name in “file.method” part, may be by using “-” to concrete class name and method name. That I have used in my example below. If you have one application, then you can use class name as an application name and use “file.method” as method name. That is totally dependent on project structure.

Trace id - A trace ID is created when a request from outside of the system is directed at a downstream service. Within the system, the trace ID is then passed on to the upstream services so they can use them in their log entries. To track trace id we need to do detail logger. If not needed can be ignore By passing 1.

Severity - DEBUG, INFO, WARN or ERROR

Source IP: If you don’t have source ip use 0.0.0.0

Sample

										

~# 2019-11-10-11:30:00| admin_lumen_api | AdminController.php-statecity_by_pincode_get | 1 | ERROR |0.0.0.0 | This is not an existing zip code.

~# 2019-11-10-18:30:00| User_lumen_api | UsersController.php-statecity_by_pincode_get | 1 | INFO |0.0.0.0 | state city by pincode fetched successfully.

Logger for PHP

Here I am am giving example using LUMEN, we can create logger class in any language

Step1. In our config WE will have 4 options, which type of logs we need to write. 0/1/2/3 0 - off logs, no logs will be written 1 - only error 2 - ERROR + Warning 3. All (error, warning, info/debug) In config we will have another variable for “log file path”

Step2.. First we need to install monolog in lumen via composer.

Step3.. After that we need to create a model for our respective log format. We will call this model in the head of every controller. Sp We will have a Model class “LogWrite”. There are 3 parameters need to pass: file name, method name, message. Please check logwrite.php

Step4.. In our method we need to call logwrite to write logs.

									

LogWrite::writelog($this->filename,$method,$message);

Logger for Python

Step1 - In python we have logger class that we can use by installing logger

pip install logger

After installing you will get logger.py file. You can edit this file as per your need

Step2: - Call logger method from your method

#import logger

from Logger import logger -- import logger asdsfdf

log = logger.getLogger('report')

log.info('New message for publish')

log.error('something went wrong')

 

Follow me on

Share it