1.1.0 • Published 11 months ago

@inderes/videosync-logger v1.1.0

Weekly downloads
-
License
-
Repository
-
Last release
11 months ago

@inderes/videosync-logger

Shared package library to be used in our backend services, and possibly in the frontend also.

Bases on Pino https://getpino.io

Quick start

Create a new logger instance with proper configuration:

const logger = new Logger({ context: 'MySweetShadow' });

logger.info('Fueled, these new shores burn');

logger.info('Dark past lies cold', { lyrics: 'Shadow, my sweet shadow' });

const error = new Error('To you, I look no more...');
logger.error('The end', error);
[
  {
    "level": "info",
    "time": "2023-02-02T07:16:16.209Z",
    "context": "MySweetShadow",
    "message": "Fueled, these new shores burn"
  },
  {
    "level": "info",
    "time": "2023-02-02T07:16:16.209Z",
    "context": "MySweetShadow",
    "data": { "lyrics": "Shadow, my sweet shadow" },
    "message": "Dark past lies cold"
  },
  {
    "level": "error",
    "time": "2023-02-02T07:16:16.209Z",
    "context": "MySweetShadow",
    "data": {
      "error": {
        "type": "Error",
        "message": "To you, I look no more...",
        "stack": "Error: To you, I look no more...\n    at Object.<anonymous> (file:///Users/pitkane/code/inderes/videosync-monorepo/apps/videosync-api/src/index.ts:87:19)"
      }
    },
    "message": "The end"
  }
]

Logger options

name

Assign name for the logger. Name is printed on every log line. This helps to identify the source of the logging. In example API might have integration modules or classes, which can identify themselves by assigning name for the class.

const logger = new Logger({ name: 'HulabaloozaIntegration' });

prettyPrint

Default: false. Normally the output is JSON. Setting this true will change to output into string, and removes some other normally outputted information. Useful for local development.

requestId

const logger = new Logger({ requestId: uuid() });

Useful if you need to track the logs. Eg. in fastify/express server, start of the request you create a logger, set the tracindId, and assign the logger to request context. All of the processing and log outputs of the single request (request context) will include the same requestId. So with one id you will get the whole trace of the request from start to finish.

Fastify does this automatically. The example below is one GET request to /api/v1/examples, and it has the same requestId the whole way of the request.

{"level":"info","time":"2022-07-27T08:16:45.654Z","name":"MySweetShadow","requestId":"ifK-HtSbgf9ZjQkMnA7W7","requestId":{"method":"GET","url":"/api/v1/examples","hostname":"localhost:3001","remoteAddress":"127.0.0.1","remotePort":55547},"message":"incoming request"}
{"level":"info","time":"2022-07-27T08:16:45.718Z","name":"MySweetShadow","requestId":"ifK-HtSbgf9ZjQkMnA7W7","moro":"jorma","message":"just testing logging"}
{"level":"info","time":"2022-07-27T08:16:45.718Z","name":"MySweetShadow","requestId":"ifK-HtSbgf9ZjQkMnA7W7","res":{"statusCode":200},"responseTime":64.16495898365974,"message":"request completed"}

defaultData

Object which is spread into every log object/line.

const logger = new Logger({ defaultData: { some: 'data' } });
logger.info('Hello world');
{
  "level": "info",
  "time": "2022-07-27T08:19:49.428Z",
  "payload": {
    "some": "data"
  },
  "message": "Hello world"
}

redactFields

Removes wanted or sensitive fields from the output log object. For example we don't want to log out passwords, which are automatically redacted.

const logger = new Logger({ redactFields: ['secretString'] });
logger.info('secrets', { username: 'super', secretString: 'secret' });
{
  "level": "info",
  "time": "2022-07-27T08:22:11.868Z",
  "payload": {
    "username": "super",
    "secretString": "[Redacted]"
  }
}

Publishing to npm

When doing changes to the package, you need to publish the package to npm. This is done with changesets. Changesets are used to create a changelog, and to publish the package to npm.

Create a changeset

npx changeset

If will ask you a series of questions, and then it will create a changeset file. This file is used to create the changelog, and to publish the package to npm.

Publish the package

After the changeset file is created, you need to commit it to git. Then you can publish the package to npm.

To bump the version:

npx changeset version

This will bump the version.

And finally publish the package to npm:

npx changeset publish
1.1.0

11 months ago

1.0.5

1 year ago

1.0.4

1 year ago

1.0.3

1 year ago

1.0.2

1 year ago

1.0.1

1 year ago

1.0.0

1 year ago