@ucd-lib/logger
v0.0.7
Published
Simple JSON logger based on a combo of Bunyan and GC Logging [LogEntry spec](https://cloud.google.com/logging/docs/reference/v2/rest/v2/LogEntry)
Readme
logger
Simple JSON logger based on a combo of Bunyan and GC Logging LogEntry spec
Contents:
Usage
const {createLogger} = require('@ucd-lib/logger');
const logger = createLogger({
name : 'my-logger'
});
logger.info('Hello World');Options
| Option | Env Var | Default Value |
|----------|----------|----------|
| name | LOG_NAME | ucdlib-logger |
| level | LOG_LEVEL | info |
| hostname | LOG_HOSTNAME | os.hostname() |
| labelsKey | LOG_LABELS_KEY | logging.googleapis.com/labels |
| labelsProperties | LOG_LABELS_PROPERTIES | name, hostname, corkTraceId |
| errorKeys | LOG_ERROR_KEYS | e, err, error |
| timeProperty | LOG_TIME_PROPERTY | time |
| noInitMsg | LOG_NO_INIT_MSG | false |
name: The name of the logger. This will be the name of the log stream in the JSON output.level: The log level. One oftrace,debug,info,warn,error,fatal. All calls to the logger will be logged at this level or higher.hostname: The hostname of the machine running the logger.labelsKey: The key to use for labels in the JSON output. Set tofalseto disable labels and all label properties will stay in the root of the JSON output. Default islogging.googleapis.com/labelswhich is the key used by Google Cloud Logging.labelsProperties: A comma separated list of properties to include in the labels object.errorKeys: A comma separated list of keys to look for in the log message objects for an anErrorobject. If an error object is found, it will be serialized and added to the log message.timeProperty: The property to use for the time in the log message. Default istime.
How it works.
You can log as many arguments as you want. Each argument should be a string or an object. All objects passed will be merged into a single object. All strings will be concatenated into a single string sperated by a ; and set as the message property of the log object. The following properties will be added to the log object:
message: The string representation of all string arguments passed to the logger.name: The name of the logger.hostname: The hostname of the machine running the logger.time: The current time in ISO format.severity: The log level. Based on the logger function called.httpRequest: Only if thereqandresobjects are passed (see more below).
Additionally labelsProperties will be moved to the labelsKey property if set.
Logging Requests
There is built in middleware for logging requests.
Example:
import express from 'express';
import {logReqMiddleware, createLogger} from '@ucd-lib/logger';
const app = express();
const logger = createLogger({
name : 'my-logger'
});
app.use(logReqMiddleware(logger));
app.get('/', (req, res) => {
res.send('Hello World');
});
app.listen(3000, () => {
logger.info('Server running on port 3000');
});This middleware will:
- Format the
reqandresobjects as the LogEntry HttpRequest object. - Timing information will be added to the log message, added to the
latencyproperty of thehttpRequestobject. - The log message will be logged at the
INFOlevel unless the response status code is 5xx, in which case it will be logged at theERRORlevel. - Check for the
corkTraceIdproperty on thereqobject orcork-trace-idin the headers. If found, add it to the log message as a label. If thecorkTraceIdproperty is not found, a newcorkTraceIdwill be generated and added to thereqobject,cork-trace-idadded to thereq.header(for use by a later proxy), added to the log message as a label.
Alternatively, you could, though not receommended, log the request manually:
logger.info({req, res, reqTimeInMs});Options: | Option | Env Var | Default Value | |----------|----------|----------| | [not available ] | LOG_REQ | true | | ignore | LOG_REQ_IGNORE | null | | debug | LOG_REQ_DEBUG | null |
LOG_REQ: Env var only. Set tofalseto disable request logging. Default istrue. This is usefule for environments such as Google Cloud Run where the request logs are already captured.ignore: Array of RegExp objects or a single string with comma separated regular expressions. If the request path matches any of the RegExps, the request will not be logged.debug: Array of RegExp objects or a single string with comma separated regular expressions. If the request path matches any of the RegExps, the request will be logged at theDEBUGlevel.
