Documentation chapters
You're looking at legacy documentation.
New multi-language documentation available at https://docs.deepkit.io
Framework

Logger

Deepkit Logger is a standalone library with one primary class Logger that you can use to log information. This class is automatically provided in the dependency injection container in your Deepkit Framework application.

The Logger class has several logging methods, each behaving like console.log.

NameLog levelLevel id
logger.error()Error1
logger.warning()Warning2
logger.log()Default log3
logger.info()Special information4
logger.debug()Debug information5

By default a Logger has a level of "info", which means it only handles info messages and above (i.e. log, warning, error, but not debug). To change the log level, call for example logger.level = 5.

Use in application

To use the logger in your Deepkit Framework application, you can simply inject Logger in your services or controllers.

import { Logger } from '@deepkit/logger';
import { injectable } from '@deepkit/injector';

class MyService {
    constructor(protected logger: Logger) {}

    doSomething() {
        const value = 'yes';
        this.logger.log('This is wild', value); 
    }
}

Colors

The logger supports colored log messages. You can provide colors by using XML tags surrounding the text that should be in color.

const username = 'Peter';
logger.log(`Hi <green>${username}</green>`);

For transporters that don't support colors, the color information is automatically removed. In the default transporter (ConsoleTransport) the color is displayed. The following colors are available: black, red, green, blue, cyan, magenta, white, and grey/gray.

Transporter

You can configure a single or multiple transporter. In a Deepkit Framework application the transporter ConsoleTransport is automatically configured. To configure additional transporters, you can use compiler passes:

import { Logger, LoggerTransport } from '@deepkit/logger';


export class MyTransport implements LoggerTransport {
    write(message: string, level: LoggerLevel, rawMessage: string) {
        process.stdout.write(JSON.stringify({message: rawMessage, level, time: new Date}) + '\n');
    }

    supportsColor() {
        return false;
    }
}

new App()
    .setup((module, config) => {
        module.setupProvider(Logger).addTransport(new MyTransport);
    })
    .run();

To replace all transporters with a new set of transporters, use set

import { Logger } from '@deepkit/logger';

new App()
    .setup((module, config) => {
        module.setupProvider(Logger).setTransport([new MyTransport]);
    })
    .run();

JSON Transport

To change the output to JSON logs, you can use the included JSONTransport.

import { Logger, JSONTransport } from '@deepkit/logger';

new App()
    .setup((module, config) => {
        module.setupProvider(Logger).setTransport([new JSONTransport]);
    })
    .run();

Formatter

Formatters allow you to change the message format, like adding the timestamp as string. When an application starts via server:start, a DefaultFormatter is automatically added (that adds timestamp, scope, and log level) when no other formatter is present.

Scoped Logger

Scoped loggers add an arbitrary scope name to each log entry, which can be helpful to diagnose which subsection of your application the log entry comes from.

const scopedLogger = logger.scoped('database');
scopedLogger.log('Query', query);

Context Data

To add contextual data to a log entry, add a plain object literal as the last argument. Only log calls with at least two arguments can have contextual data.

const query = 'SELECT *';
const user = new User;
logger.log('Query', {query, user}); //last argument is context data
logger.log('Another', 'wild log entry', query, {user}); //last argument is context data

logger.log({query, user}); //this is not handled as context data.
Made in Germany