Skip to content

Commit

Permalink
Create README.md
Browse files Browse the repository at this point in the history
  • Loading branch information
prathap-safe committed Aug 30, 2022
1 parent 2fa493d commit 4c87be6
Show file tree
Hide file tree
Showing 3 changed files with 63 additions and 4 deletions.
60 changes: 60 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
# logger

This package provides the logging configuration typically used in Safe Security.

## Motivation

Currently in Safe Security, all the services that require logging functionality imported a library & configured it as per the service needs.
Although this seems _okay_ initially, it becomes a challenge to do this across multiple services as care needs to be taken to keep the
dependency versions consistent across, code reusability takes a back seat since the configuration is duplicated everywhere, and is prone to human errors.

In order to solve this problem, the `winston` library used currently has been encapsulated in this package along with the desired defaults set up so that
the client does not have to worry about setting up a verbose configuration in multiple files.

## Installation
```bash
npm install logger-safe-security
```

```bash
yarn add logger-safe-security
```

## Usage
The introduction of this package makes it very easy for clients to consume & leverage the logging capabilities.

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

In Typescript:
```js
import { logger } from "logger-safe-security";
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 serviceLogger = logger.child({ service: "sample" });
serviceLogger.info("This log line will include service metadata");
```

## Definition
The logger exported as part of this package contains the following definition:
- Log format:
- The default log format is set to `JSON`. Hence, all the log messages will be represented (printed) as JSON objects.
- Timestamp will be added to all log lines in `yyyy-MM-dd'T'HH:mm:ss.SSSZ` format.
- Application errors will be captured in the logs.
- String interpolation is supported.
- Log level:
- The defeault 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.
- Metadata:
- `{ "type" : "application" }` is added as default metadata to easily identify application logs. This can further be extended to differentiate
from audit logs and add parsing/filtering rules (for e.g. in Datadog) as needed.
3 changes: 1 addition & 2 deletions lib/index.d.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,2 @@
import winston from "winston";
declare const logger: winston.Logger;
export default logger;
export declare const logger: winston.Logger;
4 changes: 2 additions & 2 deletions lib/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,10 @@ var __importDefault = (this && this.__importDefault) || function (mod) {
return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.logger = void 0;
const winston_1 = __importDefault(require("winston"));
const { combine, errors, timestamp, splat, json } = winston_1.default.format;
const logger = winston_1.default.createLogger({
exports.logger = winston_1.default.createLogger({
// default log level is "info"
level: "info",
// combining multiple formats to get the desired output
Expand All @@ -30,4 +31,3 @@ const logger = winston_1.default.createLogger({
// generic metadata applied to all logs
defaultMeta: { type: "application" }
});
exports.default = logger;

0 comments on commit 4c87be6

Please sign in to comment.