New Case Study:See how Anthropic automated 95% of dependency reviews with Socket.Learn More
Socket
Sign inDemoInstall
Socket

@lightbase/logger

Package Overview
Dependencies
Maintainers
6
Versions
10
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

@lightbase/logger

Simple logger in NDJSON format

  • 2.1.0
  • latest
  • Source
  • npm
  • Socket score

Version published
Maintainers
6
Created
Source

Logger

A simple logging utility library

yarn add @lightbase/logger
# npm install @lightbase/logger

Features

  • Log to any writable stream
  • Only stringify to a maximum property depth
  • Pretty print for development & NDJSON (New line delimited JSON) for production

Example

const { log, Logger } = require("./lib");

log.info("Hello");
log.info("Hello object", { my: "object" });
log.info("Hello array", ["my", "array"]);
log.info({ object: "only" });
log.info(["array", "only"]);

const myExtendedLogger = Logger.new({ context: { custom: "info" } });
myExtendedLogger.info(context);

Production output:

{"level":"info","timestamp":"2019-05-26T14:28:46.989Z","message":"Hello"}
{"level":"info","timestamp":"2019-05-26T14:28:46.990Z","message":"Hello object","my":"object"}
{"level":"info","timestamp":"2019-05-26T14:28:46.990Z","message":"Hello array","values":["my","array"]}
{"level":"info","timestamp":"2019-05-26T14:28:46.990Z","object":"only"}
{"level":"info","timestamp":"2019-05-26T14:28:46.991Z","values":["array","only"]}
{"level":"info","timestamp":"2019-05-26T14:28:46.992Z","message":"Same result as before, except the data from extraObj is added to the output.","custom":"info"}

Development output: Development output console

Check the examples folder for simple integrations with Koa and Express

API

  • Types
  • Exports

Types

LogFunction: Function

Function signature used in Logger.

(message: string | any, arg?: any | undefined | null): void

LogData: Object

Object to pass around log information

KeyTypeInfo
levelLogLevelThe level of this log information
timestampDateThe date of when the log function was called
messagestring?Standard message property
[s: string]anyAny other piece of information that is passed to the LogFunction

LogLevel: Enum

The level of the data is is logged

KeyValue
Infoinfo
Errorerror

NewLoggerOpts: Object

Options to construct a new Logger

KeyTypeDefaultInfo
logDepthnumber?5The maximum depth of object properties that are serialized
contextany?{}The context of the logger, this is appended to all logs
streamWriteStream?process.stdoutThe stream to write the logs to
isProductionboolean?NODE_ENV === productionWrite NDJSON or pretty print

NewMiddlewareOpts: Object extends NewLoggerOpts

The object to be passed to expressMiddleware and koaMiddleware functions

Formatter: Class

Formatter::RESERVED_KEYS: string[]

Keys that are reserved by the Formatter. If an object comes by with those keys, an underscore (_) is added in front of the key.

Formatter#constructor: Function

Create a new formatter instance

KeyTypeDefault
logLevelLogLevel
contextany
maxDepthnumber5

Formatter#format: Function

Combines level, time, context, message and arg into LogData and returns the result

KeyTypeDefault
messagestringany
argany?undefined

The format function returns the following for the specified inputs.

Base result: {level: LogLevel, timestamp: Date, ...context }

message Inputarg inputoutput
stringundefined{ message: string }
stringarray{ message: string, values: array }
stringany{ message: string, ...any }
arrayundefined{ values: array }
anyundefined{ ...any }

Logger: Class

Logger::defaults: Object

The default NewLoggerOpts as specified

Logger::new: Logger

Combines the defaults with the provided argument and returns a new Logger instance

(opts: NewLoggerOpts): Logger

Logger#constructor: Function

Constructs a new logger instance

KeyTypeInfo
infoFormatterThe formatter used for info messages
errorFormatterThe formatter used for error messages
writerWriterThe writer that is used

Logger#replaceWriter: (writer: Writer): void

Replace the curren writer with the provided one. For example useful when using a staging environment and setting NODE_ENV=staging which means by default the DevWriter will be used. Or when testing, to easily use a mock.

Logger#info: LogFunction

Use the info formatter and write to writer

Logger#error: LogFunction

Use the error formatter and write to writer

Logger#createChild: (context: any): Logger

Create a new logger combining the current context and the provided context

Writer: Class

Writer#constructor: Function

Creates a writer for the provided stream.

KeyTypeDefault
streamWriteStreamprocess.stdout
shouldCloseOnExitbooleanfalse

Writer#write: Function

Writes the provided data to the stream.

(data: LogData): void

DevWriter: Class extends Writer

Pretty print writer useful in development.

DevWriter::formatTime: Function

Format a date in the following format: HH:MM:SS.mmm

(time: Date): string

DevWriter#write: Function

Pretty prints the provided log data

Exports

All types are exported

log: Logger

A log instance with all the defaults

expressMiddleware: Function

(opts: NewMiddlewareOpts): ExpressMiddleware

An express middleware that logs request and response info.

Also creates a new logger and adds it to req.log & res.log with a unique requestId in the context. To easily follow log messages for a specific request

koaMiddleware: Function

(opts: NewMiddlewareOpts): KoaMiddleware

A Koa middleware that logs request and response info. Also add ctx.log with a unique requestId as context to easily follow log messages for a specific request.

Keywords

FAQs

Package last updated on 05 Sep 2019

Did you know?

Socket

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc