logstar
v3.1.0 autoconfigure isomorphic logging loggly universal winston
logstar
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.
Usage
import * as Logstar from 'logstar';
Logstar.debug('foo');
Logstar.info('bar');
Logstar.fatal('error', new Error('error'));
API
class Logger
import { Logger } from 'logstar';
constructor(options = {})
Configure loggly here. Optionally provide a globalMeta
object to be attached to each log request.
{
logLevel: 'info',
loggly: {
token: '',
subdomain: '',
tags: '',
},
globalMeta: {}
}
For JSON-logging, the following rules apply:
- 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.
debug(...args)
debugf(format, ...args)
info(...args)
infof(format, ...args)
warn(...args)
warnf(format, ...args)
error(...args)
errorf(format, ...args)
fatal(...args)
fatalf(format, ...args)
transactionLogger
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');
default
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"]}
Configuration
ENV VARIABLES
LOG_LEVEL="debug" # Defaults to info
LOGGLY_SUBDOMAIN="mysubdomain"
LOGGLY_TAGS="api-server,production"
LOGGLY_TOKEN="secret-loggly-token-here"