A (fairly) simple logging library for logging primarily to stdout, written in TypeScript with adjustable, type-safe log levels.
You're gonna need Typescript 3.4 for this to work.
Lots of loggers just do too damn much and bring in like 40 packages. I don't need 40 packages. Neither do you.
I need a logger that supports:
- Subloggers
- Context (metadata)
- Type-safety
- Easy and simple to compose your own best solution from basic function primitives
Additionally, I decided it'd be nice to offer:
- User configurable levels
- Support for chained VErrors (see https://github.com/joyent/node-verror)
- Comes with safe-json out of the box
The following are non-goals:
- Log to file, file rotation
- Streams everywhere
- Heavy dependencies
- Colors
import { logger } from 'holz'
logger.info('hi')
// stdout: {"msg":"hi","level":"info"}
// tty stdout: [info] hi
Want more customizability than that? Ok.
import { makeLogger, addContext, safeJson } from 'holz'
const logger = makeLogger({
// highest to lowest
levels: ['flark', 'blurb', 'clem'],
minLevel: "blurb",
transforms: [
addContext(() => ({ time: Date.now() })),
],
outputs: (obj) => console.log(safeJson(obj))
})
logger.flark('whatever')
// {"msg":"whatever","time":12321412312,"level":"flark"}
logger.blurb({ name: "Bob" })
// {"name":"Bob","time":1231242412,"level":"blurb"}
logger.clem("oh no")
// no output, because the minLevel is set to blurb