Skip to content

Helps you format fastify's log into a nice one line message including errors

License

Notifications You must be signed in to change notification settings

subnoto/fastify-logger

 
 

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

65 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

@subnoto/fastify-logger

CI NPM version neostandard javascript style


This is a modified version of @fastify/one-line-logger that includes error message support.

You need to make sure logged errors are already formatted as object as pino will simplify stringy them and you'll end up with an empty object for the default Error class.

You can use this in your code:

export const serializeError = (error?: Error) =>
    error ? JSON.stringify(error, Object.getOwnPropertyNames(error)) : "{}";

@subnoto/fastify-logger helps you format fastify's log into a nice one-line message:

YYYY-MM-dd HH:mm:ss.SSSTZ - <level> - <method> <route path> - <message>

A standard incoming request log line like:

{"level": 30,"time": 1660151282194,"pid": 1557,"hostname": "foo","reqId": "req-1","req": {"method": "GET","url": "/path","hostname": "localhost:8080","remoteAddress": "127.0.0.1"},"msg": "incoming request"}

Will format to:

2022-08-11 01:08:02.194+0100 - info - GET / - incoming request

Install

npm i @subnoto/fastify-logger

Getting started

const server = fastify({
    logger: {
        transport: {
            target: "@subnoto/fastify-logger",
        },
    },
});

Colors

Colors are enabled by default when supported. To manually disable the colors you need to set the transport.colorize option to false. For more options check the colorette docs.

const server = fastify({
    logger: {
        transport: {
            target: "@subnoto/fastify-logger",
            colorize: false,
        },
    },
});

Custom levels

Custom levels could be used by passing it into logger opts

const server = fastify({
    logger: {
        transport: {
            target: "@subnoto/fastify-logger",
        },
        customLevels: {
            foo: 35,
            bar: 45,
        },
    },
});

server.get("/", (request, reply) => {
    request.log.info("time to foobar");
    request.log.foo("FOO!");
    request.log.bar("BAR!");
    reply.send({ foobar: true });
});

Custom level colors

Custom level colors can be used by passing it into logger opts. They can also overwrite the default level's colors. Check all the supported colors here.

const server = fastify({
    logger: {
        transport: {
            target: "@subnoto/fastify-logger",
            colors: {
                35: "bgYellow",
                45: "magenta",
                60: "bgRedBright", // overwriting the `fatal` log color
            },
        },
        customLevels: {
            foo: 35,
            bar: 45,
        },
    },
});

server.get("/", (request, reply) => {
    request.log.fatal("An error occured");
    request.log.foo("FOO!");
    request.log.bar("BAR!");
    reply.send({ foobar: true });
});

License

Licensed under MIT.

About

Helps you format fastify's log into a nice one line message including errors

Topics

Resources

License

Stars

Watchers

Forks

Contributors

Languages

  • JavaScript 96.4%
  • TypeScript 3.6%