0.2.0 • Published 8 years ago

g-log v0.2.0

Weekly downloads
2
License
ISC
Repository
github
Last release
8 years ago

g-log

npm install --save g-log
const GLog = require('g-log');

GLog.info('The default logger prints to the console');

// => [1491231854012][INFO] The default logger prints to the console

var child = GLog.child({ $tags: 'APP' });
child.info('Create child logs with tags and metadata');

// => [1491231854012][INFO][APP] Create child logs with tags and metadata

var fileTransport = new GLog.Transports.File('path/to/my/log.log');
child.transports.push(fileTransport);

child.info('This log will go to both the console, and the log file!', { meta: 'data' });

// => [1491231854012][INFO][APP] This log will go to both the console, and the log file!
// => {"$tags":["APP"],"meta":"data","time":1491231854012,"level":"info","text":"This log will go to both the console, and the log file!"}

var nextChild = GLog.child({ $tags: 'COMPONENT' });
nextChild.info('Tags are preserved when creating children');

// => [1491231854012][INFO][APP][COMPONENT] Tags are preserved when creating children
// => {"$tags":["APP","COMPONENT"],"meta":"data","time":1491231854012,"level":"info","text":"Tags are preserved when creating children"}

Metadata

Metadata is attached to logs as simple javascript objects. There is currently one reserved key, $tags which is an array of strings used as tags for the log message. When creating child logs, this key is created by concatenating the parent's Logger#meta.$tags with the child's.

API

Modules

Classes

GLog : Logger

The main exports. GLog is an instance of Logger, so it will have all associated properties and methods.

GLog.version : string

Kind: static property of GLog
Read only: true

GLog.Logger : Logger

A reference to the Logger class

Kind: static property of GLog
Read only: true

GLog.Transport : Transport

A reference to the Transport class

Kind: static property of GLog
Read only: true

GLog.Transports : object

A reference to the list of transports

Kind: static property of GLog
Read only: true

Transports.Console : Console

Kind: static property of Transports

Transports.File : File

Kind: static property of Transports

Transports.Stream : Stream

Kind: static property of Transports

GLog.exists : function

A reference to exists

Kind: static property of GLog
Read only: true

GLog.get : function

A reference to get

Kind: static property of GLog
Read only: true

GLog.remove : function

A reference to remove

Kind: static property of GLog
Read only: true

GLog.levels : object

A reference to levels

Kind: static property of GLog
Read only: true

GLog.create ⇒ Logger

Convenience method for creating new loggers.

Kind: static property of GLog

ParamTypeDescription
levelnumberThe level to log at
metaobjectMetadata to attach to all messages
configobjectAdditional configuration for the logger
config.transportsarrayWhere to send our log messages.

Logger

Kind: global class

new Logger(level, meta, config)

Our core logger class. Contains the logging methods, and allows you to create child logs. In addition to the listed methods, there will be a method for each logging level, that simply redirects to log with a set level argument (these are generated by setLevels).

ParamTypeDescription
levelnumberThe level to log at
metaobjectMetadata to attach to all messages
configobjectAdditional configuration for the logger
config.namestringA name that we can use to retrieve our log.
config.transportsarrayWhere to send our log messages.

logger.level

The current logging level. Will be converted to a number when set with a string.

Kind: instance property of Logger
Properties

NameType
levelnumber

logger.remove

Removes the reference to this log from logs.

Kind: instance property of Logger

logger.mergeMeta() ⇒ object

Merges any number of metadata objects together.

Kind: instance method of Logger
Returns: object - The merged metadata.

logger.log(level, msg, logmeta)

Logs a message to the current transports if and only if Logger#level is greater than or equal to level.

Kind: instance method of Logger

ParamTypeDescription
levelstringThe name of the current level.
msgstringThe message to be logged.
logmetaobjectAdditional metadata to be logged.

logger.child(level, meta, config)

Creates a new logger using all of the same settings as the parent, unless modified in config. Also merges meta with the parent's metadata.

Kind: instance method of Logger

ParamTypeDescription
levelnumberThe initial level to log at.
metaobjectPermanent metadata to add to the parent's.
configobjectExtra configuration (overwrite parent's).

Logger.levels : object

A set of levels in the form name: level. Lower level means more important (error is usually 0). This property should not be set directly, instead use setLevels.

Kind: static property of Logger

Logger.logs : object

A container that holds all instantiated logs.

Kind: static property of Logger

Logger.exists(name) ⇒ boolean

Checks whether the named log exists.

Kind: static method of Logger

ParamType
namestring

Logger.get(name, defaultLevel) ⇒ Logger

Returns the log with name, instantiating it if necessary.

Kind: static method of Logger

ParamType
namestring
defaultLevelstring

Logger.remove()

Removes the named log from logs.

Kind: static method of Logger

Logger.setLevels(levels)

Sets the levels and creates the corresponding methods used by all logs (including already instantiated ones).

Kind: static method of Logger

ParamTypeDescription
levelsobjectA set of levels in the form name: level. Lower level means more important (error is usually 0).

Logger.nextid() ⇒ number

Returns a (usually) unique id

Kind: static method of Logger

Transport

Kind: global class

new Transport(level)

Base class for transports.

ParamTypeDescription
levelnumberThe level to log at.

transport.level

The current logging level. Will be converted to a number when set with a string. This can be set independently of the logger's level (but the logger won't pass on any messages above its own level).

Kind: instance property of Transport
Properties

NameType
levelnumber

transport.log(string, data)

Implemented by subclasses. What we actually do with the log message once it's ready.

Kind: instance abstract method of Transport

ParamTypeDescription
stringstringThe message in JSON form.
datastringThe message in object form.

Console ⇐ Transport

Kind: global class
Extends: Transport

new Console(level, formatter)

Outputs log messages to the console, via a formatter function.

ParamTypeDescription
levelnumberThe level to log at.
formatterfunctionA function that accepts the object form of the message, and returns a string to be logged via console.log.

console.level

The current logging level. Will be converted to a number when set with a string. This can be set independently of the logger's level (but the logger won't pass on any messages above its own level).

Kind: instance property of Console
Properties

NameType
levelnumber

console.format(message) ⇒ string

The default format function.

Kind: instance method of Console

ParamTypeDescription
messageobjectThe object form of a message to be logged.

console.log(string, data)

Logs a message to the console.

Kind: instance method of Console
Overrides: log

ParamTypeDescription
stringstringThe message in JSON form.
datastringThe message in object form.

File ⇐ Transport

Kind: global class
Extends: Transport

new File(level, path, options)

Outputs log messages to a file as line-delimited JSON.

ParamTypeDescription
levelnumberThe level to log at.
pathstringThe path to the log file.
optionsobjectAdditional options to pass to fs.createWriteStream.

file.level

The current logging level. Will be converted to a number when set with a string. This can be set independently of the logger's level (but the logger won't pass on any messages above its own level).

Kind: instance property of File
Properties

NameType
levelnumber

file.log(string)

Writes the JSON form of a log message to the file stream.

Kind: instance method of File
Overrides: log

ParamTypeDescription
stringstringThe message in JSON form.

Stream ⇐ Transport

Kind: global class
Extends: Transport, node.Stream.Readable

new Stream(level)

Outputs log messages as a readable stream.

ParamTypeDescription
levelnumberThe level to log at.

stream.level

The current logging level. Will be converted to a number when set with a string. This can be set independently of the logger's level (but the logger won't pass on any messages above its own level).

Kind: instance property of Stream
Properties

NameType
levelnumber

stream.log(string, data)

Pushes the log message into the stream, delimited with a newline.

Kind: instance method of Stream
Overrides: log

ParamTypeDescription
stringstringThe message in JSON form.
datastringThe message in object form.