-
JSON Logs: JSON logging is recommended for production deployments as it provides more detailed information.
JSON logs include fields such as time
, source
, request
, extraData
, and stack
.
To enable JSON logging, use the following configuration:
applicationLogger.setJsonLogging(true);
.
.
.
loggerInstance.error('This is test log');
Output:
{
"level": "error",
"time": "2024-07-01T07:07:54.877Z",
"className": "Application:Instance1",
"source": {
"caller": "callerFunctionName",
"fileName": "fileName.ts",
"path": "file path",
"line": "13",
"column": "10"
},
"message": "This is a test log",
"request": {},
"extra": {},
"stack": ""
}
This format ensures that logs contain comprehensive information, making it easier to debug and monitor applications in production environments.
-
Github Link: This feature enhances the logging library by providing a direct GitHub link that redirects to the
exact location in the code where the log is generated. This allows users to easily navigate the entire codebase from the logs.
const applicationLogger = new Logger4Node(
'Application',
{
github: {
org: 'yog27ray',
repo: 'logger4node',
commitHash: 'githubCommitHash',
basePath: 'project/root/path'
},
});
- commitHash: The specific commit hash of the code.
- org: The GitHub organization name.
- repo: The name of the repository.
- AbasePath: The absolute path to the root folder of the project.
By configuring these parameters, the logger will generate links that point to the exact lines in the GitHub repository, simplifying code navigation and debugging.
-
Multiple Logging Patter: This feature allows you to set different log severities for different instances of the logger.
applicationLogger.setLogLevel(LogSeverity.ERROR);
applicationLogger.setLogPattern('Application:*');
applicationLogger.setLogSeverityPattern(LogSeverity.INFO, 'Application:Instance1,Application:Instance2');
applicationLogger.setLogSeverityPattern(LogSeverity.DEBUG, 'Application:Instance3');
In the example above:
- The default logging level is set to ERROR for all application logs.
- For the logger instances Instance1 and Instance2, logs will include INFO level messages.
- For the logger instance Instance3, logs will include DEBUG level messages.
This setup enables more granular control over logging by specifying different severity levels for specific logger instances.
-
Track Session Log: Often, it’s useful to track all logs that correspond to a single session to better understand
the code flow. You can also pass additional information to be preserved throughout the session. Below is the
configuration to enable this feature:
-
With Express Server:
app.use(Logger4Node.Trace.requestHandler((req: Request) => {
const sessionInformation = { user: 'userId' };
return sessionInformation;
}));
.
.
.
loggerInstance.error('This is test log');
After enabling this configuration, every log will include request.id
and sessionInformation
in the request object.
This helps track all logs generated by an HTTP call.
Example Output:
{
"level": "error",
"time": "2024-07-01T07:07:54.877Z",
"className": "Application:Instance1",
"source": {
"caller": "callerFunctionName",
"fileName": "fileName.ts",
"path": "file path",
"line": "13",
"column": "10"
},
"message": "This is a test log",
"request": {
"id": "a15e7fc5-46b2-417d-9523-37a7c4dd467e",
"user": "userId"
},
"extra": {},
"stack": ""
}
-
Without Express Server: You can also start a new session wherever you need it in your application.
Logger4Node.Trace.startNewRequest(() => {
}, { user: 'userId' });