Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: added support to include service #6

Merged
merged 3 commits into from
Sep 21, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 3 additions & 5 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -37,13 +37,11 @@ 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:
Additionally, one can pass `service` metadata while importing it within the context of a service. For example:
```js
const { createLogger } = require("logger-safe-security");
const logger = createLogger();
const serviceLogger = logger.child({ service: "sample" });
serviceLogger.info("This log line will include service metadata");
const logger = createLogger({ service: "sample" });
logger.info("This log line will include service metadata");
```

## Definition
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "logger-safe-security",
"version": "1.1.2",
"version": "1.1.3",
"description": "Custom logging framework used in SAFE",
"main": "lib/index.js",
"types": "lib",
Expand Down
16 changes: 10 additions & 6 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,11 +6,15 @@ export const levels = winston.config.npm.levels;

export type Logger = winston.Logger;

export const createLogger = ({
logLevel = "info"
}: {
logLevel: string;
} = { logLevel: "info" }): winston.Logger =>
export const createLogger = (
{
logLevel = "info",
service
}: {
logLevel?: string;
service?: string;
} = { logLevel: "info" }
): winston.Logger =>
winston.createLogger({
// default log level is "info"
level: logLevel,
Expand Down Expand Up @@ -42,5 +46,5 @@ export const createLogger = ({
],

// generic metadata applied to all logs
defaultMeta: { type: "application" }
defaultMeta: { type: "application", ...(service && { service }) }
});