MediaTech Logger
The framework aims to facilitate the logging needs of the developers in TV2 Udviklingsteknologi, as well as creating an uniform logging strategy across Typescript and Javascript projects.
The framework consists of 3 package:
@tv2media/logger
contains all the common logic used by the two other packages. Note: previously this was the main package.@tv2media/logger/node
contains specializations of the common logic for a Node environment – e.g. logic for storing logs in a file or printing to the terminal console.@tv2media/logger/web
contains specializations of the common logic for a web browser environment – e.g. logic for sending logs over a WebSocket or printing to the developer console.
Install & build
The package can be installed by:
$ yarn add @tv2media/logger
To build from source:
$ git clone https://github.com/tv2/mediatech-logger.git
$ cd mediatech-logger
$ yarn && yarn build
Usage - Simple
import { DefaultLogger } from '@tv2media/logger/node'
const logger = new DefaultLogger()
logger.info('Server started.')
logger.error('Request failed.')
logger.data(new Error('Some dangerous error!')).error('Request failed.')
Usage - Advanced
import {
LoggerBase,
Level,
Format,
ColoredPlainTextFormat,
JsonFormat,
Vault,
ConsoleVault
} from '@tv2media/logger'
const logger = DefaultLogger([
new ConsoleVault({
level: Level.INFO,
format: ColoredPlainTextFormat(),
isFormatLocked: false,
}),
new FileVault({
level: Level.INFO,
format: JsontFormat({ isPretty: false }),
isFormatLocked: true,
}),
])
logger.data('some-data')
logger.tag('some-tag')
logger.error('Server failed.')
logger.warn('No response from client.')
logger.info('Server started at ip:port')
logger.debug({ip: '0.0.0.0'})
logger.trace('some trace here')
logger.tag('testing').info('test message')
logger.info('message', {tag: 'testing', otherMeta: 'meta'})
logger.meta({hostname: 'host1', author: 'me'})
logger.setLevel(Level.info)
Environment Variables
@tv2media/logger/node
The DefaultLogger
is using the environment variable NODE_ENV to determine which log level and format which will be used. The current setup is the following.
NODE_ENV=production
NODE_ENV=stage | staging
NODE_ENV=develop | development
NODE_ENV=local
NODE_ENV="any other value"
Setting the environment variable LOG_LEVEL overrides the log level from the NODE_ENV setup, this can come in handy when you need to enable e.g. debugging logs in a production environment.
@tv2media/logger/web
The DefaultLogger
is using the environment variable ENV to determine which log level and format which will be used. The current setup is the following.
window.env.ENV=production
window.env.ENV=stage | staging
window.env.ENV=develop | development
window.env.ENV=local
window.env.ENV="any other value"
Setting the variable window.env.LOG_LEVEL
overrides the log level from the window.env.ENV
setup, this can come in handy when you need to enable e.g. debugging logs in a production environment.