Skip to content

Commit

Permalink
feat: add support for loglevels (#3)
Browse files Browse the repository at this point in the history
Co-authored-by: Aman Jain (ECS/DEL) <[email protected]>
  • Loading branch information
amanjain14 and jainaman1497 authored Sep 21, 2022
1 parent a5c2591 commit d40500f
Show file tree
Hide file tree
Showing 2 changed files with 45 additions and 41 deletions.
11 changes: 7 additions & 4 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -25,20 +25,23 @@ The introduction of this package makes it very easy for clients to consume & lev

In Javascript:
```js
const { logger } = require("logger-safe-security");
const { createLogger } = require("logger-safe-security");
const logger = createLogger({ logLevel: "info" });
logger.info("Hello world!");
```

In Typescript:
```js
import { logger } from "logger-safe-security";
import { createLogger } from "logger-safe-security";
const logger = createLogger({ logLevel: "info" });
logger.info("Hello world!");
```

Additionally, one can create child loggers inherited from the parent/package-default to pass specific metadata.
One such use case would be to add `service` metadata while importing it within the context of a service. For example:
```js
import { logger } from "logger-safe-security";
const { createLogger } = require("logger-safe-security");
const logger = createLogger();
const serviceLogger = logger.child({ service: "sample" });
serviceLogger.info("This log line will include service metadata");
```
Expand All @@ -51,7 +54,7 @@ The logger exported as part of this package contains the following definition:
- Application errors will be captured in the logs.
- String interpolation is supported.
- Log level:
- The defeault log level is `INFO`.
- The default log level is `INFO`.
- Transports:
- All logs will be written to `Console` by default.
- Exceptions & Promise Rejections will be handled & written to `Console` as well.
Expand Down
75 changes: 38 additions & 37 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,40 +2,41 @@ import winston from "winston";

const { combine, errors, timestamp, splat, json } = winston.format;

export const logger = winston.createLogger({

// default log level is "info"
level: "info",

// combining multiple formats to get the desired output
format: combine(

// required to log errors thrown by the application; ignored otherwise
errors({ stack: true }),

// enables string interpolation of messages
splat(),

// adds timestamp to all log messages
timestamp(),

// default log format is JSON
json()
),

transports: [

// logs will be written to console
new winston.transports.Console({

// catch and log `uncaughtException` events from the application
handleExceptions: true,

// catch and log `uncaughtRejection` events from the application
handleRejections: true
})
],

// generic metadata applied to all logs
defaultMeta: { type: "application" }
});
export const createLogger = ({
logLevel = "info"
}: {
logLevel: string;
}): winston.Logger =>
winston.createLogger({
// default log level is "info"
level: logLevel,

// combining multiple formats to get the desired output
format: combine(
// required to log errors thrown by the application; ignored otherwise
errors({ stack: true }),

// enables string interpolation of messages
splat(),

// adds timestamp to all log messages
timestamp(),

// default log format is JSON
json()
),

transports: [
// logs will be written to console
new winston.transports.Console({
// catch and log `uncaughtException` events from the application
handleExceptions: true,

// catch and log `uncaughtRejection` events from the application
handleRejections: true
})
],

// generic metadata applied to all logs
defaultMeta: { type: "application" }
});

0 comments on commit d40500f

Please sign in to comment.