Skip to content

Latest commit

 

History

History
87 lines (69 loc) · 3.17 KB

README.md

File metadata and controls

87 lines (69 loc) · 3.17 KB

Deno Structured Logging

deno doc deno.land

A better logger for deno, with support for structured logging.

Planned features for v1

  • Allow environment variables to control loglevels #1
  • Customisable timestamp formats #2
  • Proper serialisation of objects #3
  • Async support in sinks #4
  • More built-in sinks #5
  • Better / more complete documentation #6
  • Filtering logs per sink #7

Simple Example

import { createLogger, consoleSink } from "https://deno.land/x/[email protected]/mod.ts";

const logger = createLogger().addSink(consoleSink());

logger.debug("Debug");
logger.info("Info");
logger.warn("Warn");
logger.error("Error");
logger.critical("Critical");

Simple Example

More complex example

import { green } from "https://deno.land/[email protected]/fmt/colors.ts";
import {
  createLogger,
  LogLevel,
  consoleSink,
  fileSink,
  jsonFormat,
  textFormat,
} from "https://deno.land/x/[email protected]/mod.ts";

const logger = createLogger({
  minimumLevel: LogLevel.INFO,
  outputFormat: textFormat, // You can customise the default output format
})
  .addSink(consoleSink({
    colorOptions: { info: green }, // You can customise the log level colors
  }))
  .addSink(fileSink("log.ndjson"), jsonFormat); // You can set a custom format per sink

logger.debug("Debug"); // Ignored due to the minimumLevel
logger.info("This is {type} logging in {program}", "Structured", "Deno");

// It doesn't matter what these variables are called,
const num = 1;
const array = ["a", "b", "c"];

logger.warn("Numbers work: {number} as well as arrays: {arr}", num, array);

Complex Example

String formatting

DSL uses it's own form of string formatting, similar to Serilog in C#. The syntax for this is

logger.info("Hello {name}, this is another {variable}", "First", 2);

where "First" and 2 are substituted into {name} and {variable} respectively. With the default console sink, the names don't really matter however they help readability of the format and with more complex formats, for example a JSON format, they could be used as property names.

Available sinks

A sink is an output for DSL. For example the console sink prints to the console, or the file sink writes to a file. At this point a sink is literally just a function, so it's easy enough to create your own.

Available sinks:

  • Console Sink (with colors)
  • File Sink
  • More coming soon