1.3.9 • Published 1 year ago

sync-logger-v2 v1.3.9

Weekly downloads
-
License
ISC
Repository
-
Last release
1 year ago

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:-

  1. Adding Context ID to all logs throughout the call stack.
  2. Write logs to a file path
  3. Log rotation of log files by date and time.
  4. 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 and LogExecContext but for simple nodejs app logging we would only need Log and LogExecContext
    • 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) and loggerOptions (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.e maxSize: '50M'//5 MB 4. interval: Time interval after which a new log file is generated. (optional if "disableRotation" is true). i.e interval: '1d'

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
    • 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.

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.3.7

1 year ago

1.3.6

1 year ago

1.3.5

1 year ago

1.3.4

1 year ago

1.3.3

1 year ago

1.3.2

1 year ago

1.3.1

1 year ago

1.3.0

1 year ago

1.3.9

1 year ago

1.3.8

1 year ago

1.2.3

2 years ago

1.2.2

2 years ago

1.2.1

2 years ago

1.2.0

2 years ago

1.1.9

2 years ago

1.1.8

2 years ago

1.1.7

2 years ago

1.1.6

2 years ago

1.1.5

2 years ago

1.1.4

2 years ago

1.1.3

2 years ago

1.1.2

2 years ago

1.1.1

2 years ago

1.1.0

2 years ago

1.0.9

2 years ago

1.0.8

2 years ago

1.0.7

2 years ago

1.0.6

2 years ago

1.0.5

2 years ago

1.0.4

2 years ago

1.0.3

2 years ago

1.0.2

2 years ago

1.0.1

2 years ago

1.0.0

2 years ago