sync-logger-v2 v1.3.9
Heading 3 This Logger library is made to standardize the logging logic throught Sync's nodeJs services.
Logger is compatible with all node js applications and can also work as a API logging middleware for expressJS servers
Heading 4 Key Features:-
- Adding Context ID to all logs throughout the call stack.
- Write logs to a file path
- Log rotation of log files by date and time.
- Express JS middleware for API logging
How do I get set up?
- To install the logger library, type
npm install git+https://git@bitbucket.org/sync-developers/logger.git
in the app root terminal.
How do I use it in nodeJS?
- Sync logger exports
ExpressJSLoggerMiddleware
,Log
andLogExecContext
but for simple nodejs app logging we would only needLog
andLogExecContext
- Sync logger exports
LogExecContext
is wrapped around a function that we intend to assign a common Context ID throughout the call stack.
Log
is used to create a configurable logger object, which includes the options to add logs to file and adding file rotation
- it accepts 2 options
writeToFileOpt
(options to add logs to file) andloggerOptions
(tslog options, refer to this link) - The option
writeToFileOpt
has following configurations- 1.disableRotation
: true if you need to disable file rotation of log files over an interval of time. 1.fileName
: It is path to the log file, this option must have %{DATE} to let logger parse the current date to the log file name. i.e:fileName: './sync-logger-%{DATE}.log'
2.pattern
: It is the date format for name of the log file. It must has "YYYY", "MM", "DD" and "INDEX". i.e pattern: 'YYYY-MM-DD-INDEX', 3.maxSize
: The maximum size of the file after which a new file should be created. (optional if "disableRotation" is true). i.emaxSize: '50M'
//5 MB 4.interval
: Time interval after which a new log file is generated. (optional if "disableRotation" is true). i.einterval: '1d'
- The option
Heading 4 Example-
await LogExecContext({ requestId: uuidv4() }, async () => {
// Add your root function here to add common log context ID
});
How do I use it in expressJS?
- import
Log
from logger and declare an object in a separate file where you can use the same logger instance throughout the application
- import
- Now import
ExpressJSLoggerMiddleware
from main express file and then register it as following- -app.use(ExpressJSLoggerMiddleware(logger));
// logger is the log instance we create in a separate file in the above step.
- Now import
Heading 4 Full Express JS example:-
` import { Response, Request } from 'express'; import { ExpressJSLoggerMiddleware, Log } from 'sync-logger-v2'; import { LoggerWithoutCallSite } from 'tslog'
const express = require( "express" ); const app = express();
const logger = Log({ // You should declare this in a separate file and then import it for it to be used elsewhere for logging writeToFileOpt: { disableRotation: false, fileName: './sync-logger-%{DATE}.log', pattern: 'YYYY-MM-DD-INDEX', maxSize: '50M', interval: '1d' } });
app.use(ExpressJSLoggerMiddleware(logger));
// define a route handler for the default home page app.get( "/", ( req: Request, res: Response ) => { logger.info('Hello1') logger.info('Hello2') // Logs will happen with the Common Context ID and will be stored in the log file
res.json( "Hello world!" );
});
// start the Express server
app.listen(4000, () => {
console.log( server started at http://localhost:4000
);
});`
Who do I talk to?
- Jas
1 year ago
1 year ago
1 year ago
1 year ago
1 year ago
1 year ago
1 year ago
1 year ago
1 year ago
1 year ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago