Simple, opinionated, isomorphic logging.
It uses winston on the server and console.log
on the client. It supports multiple arguments. If you have LOGGLY environment variables set, it will auto-configure the winston loggly transport.
import * as Logstar from 'logstar';
Logstar.debug('foo');
Logstar.info('bar');
Logstar.fatal('error', new Error('error'));
import { Logger } from 'logstar';
Configure loggly here. Optionally provide a globalMeta
object to be attached to each log request.
{
logLevel: 'info',
loggly: {
token: '',
subdomain: '',
tags: '',
},
globalMeta: {}
}
- If there are exactly 2 arguments passed to the log function, and the second argument is an object, then that object is used as the
meta
parameter, and the first argument is used as the log message. - If there is exactly 1 argument passed to the log function, and that argument is an object, then that object is used as the
meta
parameter, with an empty log message. - In all other cases, the first argument is used as the log message, and the subsequent arguments are added as the
context
property on themeta
object.
Get a logger that adds a transaction_id
property to the meta. Useful for tracking related requests.
import { transactionLogger } from 'logstar';
const myLogger = transactionLogger('TRANSACTION-ID');
Logger pre-configured with loggly parameters from environment variables. (See below)
import Logstar from 'logstar';
Logstar.info('Hello', 12, { foo: 'bar' }, [1, 2, 3], new Error('Hello'));
// This will end up as:
{"level": "info", "message": "Hello", "context": [12, {"foo": "bar"}, [1, 2, 3], "Hello"]}
ENV VARIABLES
LOG_LEVEL="debug" # Defaults to info
LOGGLY_SUBDOMAIN="mysubdomain"
LOGGLY_TAGS="api-server,production"
LOGGLY_TOKEN="secret-loggly-token-here"