@fastify/one-line-logger
TypeScript icon, indicating that this package has built-in type declarations

2.0.1 • Public • Published

@fastify/one-line-logger

@fastify/one-line-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 @fastify/one-line-logger

Getting started

const server = fastify({
  logger: {
    transport: {
      target: "@fastify/one-line-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: "@fastify/one-line-logger",
      colorize: false,
    },
  },
});

Custom levels

Custom levels could be used by passing it into logger opts

const server = fastify({
  logger: {
    transport: {
      target: "@fastify/one-line-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 levels colors could 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: "@fastify/one-line-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

MIT License

Dependents (5)

Package Sidebar

Install

npm i @fastify/one-line-logger

Weekly Downloads

2,885

Version

2.0.1

License

MIT

Unpacked Size

24.8 kB

Total Files

19

Last publish

Collaborators

  • gurgunday
  • metcoder95
  • galvez
  • simenb
  • coopflow
  • simoneb
  • rafaelgss
  • starptech
  • delvedor
  • matteo.collina
  • allevo
  • jsumners
  • zekth
  • eomm
  • fox1t
  • airhorns
  • kibertoad
  • climba03003
  • fdawgs
  • ivan-tymoshenko