Skip to content

Latest commit

 

History

History
356 lines (211 loc) · 11.4 KB

API.md

File metadata and controls

356 lines (211 loc) · 11.4 KB

Table of Contents

LoggerFactory

factory.js:26-127

Handler logger and its settings manipulation. Default factory returned when you call require("huzzah").

Parameters

  • opts {handlerConstructor: function}? Options (optional, default {handlerConstructor:MultiHandler})

get

factory.js:39-46

Returns logger with given name

Parameters

Returns Logger

settings

factory.js:114-116

Returns settings for logger with given name

Parameters

Returns Handler

Meta

  • deprecated: This is deprecated.

handler

factory.js:123-126

Returns hander for logger with given name

Parameters

Returns Handler

Logger

logger.js:15-138

Produce log records. It could not be created manually, always use LoggerFactory instance to create loggers.

Parameters

  • onLogCallback function factory, holding this logger
  • name string name of this logger
  • context Object Set of keys, which will be auto-added to each record

with

logger.js:55-62

Creates new Logger with the same name, factory but with given context. Every property and value of context will be added to log record. This can be usefull for json logging

Parameters

Returns Logger new logger with given context

trace

logger.js:75-77

Create log record with level TRACE. If first argument is string, it is used as message format, like console.log do. Supported modifiers %s, %d, %j, %%. If you want to output error, it must be one among all arguments and be last.

Parameters

  • args ...any Any arguments. Error must be one and last.

Examples

logger.trace('User entered %s', userInput);

logger.error('Error happen while sending email', err);

debug

logger.js:90-92

Create log record with level DEBUG. If first argument is string, it is used as message format, like console.log do. Supported modifiers %s, %d, %j, %%. If you want to output error, it must be one among all arguments and be last.

Parameters

  • args ...any Any arguments. Error must be one and last.

Examples

logger.trace('User entered %s', userInput);

logger.error('Error happen while sending email', err);

info

logger.js:105-107

Create log record with level INFO. If first argument is string, it is used as message format, like console.log do. Supported modifiers %s, %d, %j, %%. If you want to output error, it must be one among all arguments and be last.

Parameters

  • args ...any Any arguments. Error must be one and last.

Examples

logger.trace('User entered %s', userInput);

logger.error('Error happen while sending email', err);

warn

logger.js:120-122

Create log record with level WARN. If first argument is string, it is used as message format, like console.log do. Supported modifiers %s, %d, %j, %%. If you want to output error, it must be one among all arguments and be last.

Parameters

  • args ...any Any arguments. Error must be one and last.

Examples

logger.trace('User entered %s', userInput);

logger.error('Error happen while sending email', err);

error

logger.js:135-137

Create log record with level ERROR. If first argument is string, it is used as message format, like console.log do. Supported modifiers %s, %d, %j, %%. If you want to output error, it must be one among all arguments and be last.

Parameters

  • args ...any Any arguments. Error must be one and last.

Examples

logger.trace('User entered %s', userInput);

logger.error('Error happen while sending email', err);

LEVELS

levels.js:7-15

Log levels. Log levels have a priority. ALL < TRACE < DEBUG < INFO < WARN < ERROR < OFF

NullHandler

handlers.js:13-45

Basic handler. Does not actually handle anything. Usefull for extensions and stubs. All implementors must implement _handle(record) method. record instance shared among all handlers, do not modify it.

setLevel

handlers.js:23-32

Set max accepted log level for this handler

Parameters

Returns this

MultiHandler

handlers.js:56-76

Extends NullHandler

Holds settings of one logger. It is max accepted log level and handlers

addHandler

handlers.js:68-75

Adds handler to logger

Parameters

Returns this

BaseHandler

handlers.js:86-144

Extends NullHandler

Used as base class for most handlers All extensions should implement method _handle(record)

setFormat

handlers.js:130-143

Set log record format for this handler. Default record format is [%date] %-5level %logger - %message%n%error

Possible parameters:

  • %d{FORMAT} or %date{FORMAT} - how to format record timestamp. {FORMAT} is optional stftime string, by default it is %Y/%m/%d %H:%M:%S,%L
  • %level or %le or %p - output record level name like ERROR or WARN
  • %logger or %lo or %c - output logger name
  • %% - output %
  • %n - output EOL
  • %err or %error - output stack trace of passed error
  • %x or %x{field} - output JSON.stringified value of context field

Also available text decorators (now only colors):

  • %highlight(text) - will decorate passed text with record level decorator
  • %cyan(text)
  • other colors
  • %cyan.bold(text)
  • other bold colors

Example: %date000 %highlight(%5level) %cyan(%logger) - %message%n%error

Also it is possible to set it to function. One of possible examples jsonFormat (require('huzzah/json-format') ). It can accept the same serializers bunyan can accept and you will have json output at the end.

Parameters

  • format (string | function) It is either formatted string as described, or function returning string and accepts just one argument log record

Examples

var f = new LoggerFactory();

var logger = f.get('a.b');

f.settings('a.b')
  .addHandler(new ConsoleHandler().setFormat(jsonFormat({
    timestamp: strftimeCompile('%Y/%m/%d %H:%M:%S,%L000')
  })));

Returns this

ConsoleHandler

handlers.js:151-160

Extends BaseHandler

Just simple console handler

RawConsoleHandler

handlers.js:168-180

Extends BaseHandler

Like ConsoleHandler but output whole record to console. This can be usefull in browser to see inspections.

StreamHandler

handlers.js:187-218

Extends BaseHandler

Allow to pass records to stream

setShouldFormat

handlers.js:204-207

Should we format record before passing to stream? By default it is true

Parameters

Returns this

setStream

handlers.js:214-217

Stream to pass records

Parameters

  • stream WritableStream

Returns this