From 8d2dfbab7667bef775f220f2d88c0f1f4aab7805 Mon Sep 17 00:00:00 2001 From: Ruben Beltran del Rio Date: Mon, 21 Sep 2020 01:07:52 +0200 Subject: Update docs and tooling --- doc/COVERAGE.md | 272 ++++++++++++++++++++++++++++++++ doc/README.md | 476 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 748 insertions(+) create mode 100644 doc/COVERAGE.md create mode 100644 doc/README.md (limited to 'doc') diff --git a/doc/COVERAGE.md b/doc/COVERAGE.md new file mode 100644 index 0000000..8127492 --- /dev/null +++ b/doc/COVERAGE.md @@ -0,0 +1,272 @@ +# TOC + - [test/cologne.js](#testcolognejs) + - [test/utilities.js](#testutilitiesjs) + - [test/formatters/simple.js](#testformatterssimplejs) + - [test/formatters/token.js](#testformatterstokenjs) + - [test/loggers/file.js](#testloggersfilejs) + - [test/loggers/console.js](#testloggersconsolejs) + + + +# test/cologne.js +#log() should send every argument to the loggers. + + +#log() should send the same arguments to all the loggers. + + +#log() should send all objects in cologne log format. + + +#log() should default to level 6. + + +#debug() should set to level 7. + + +#info() should set to level 6. + + +#notice() should set to level 5. + + +#warn() should set to level 4. + + +#error() should set to level 3. + + +#buildLog() should return a cologne log. + + +#buildLog() should default to level 6. + + +#buildLog() should use the specified level. + + +#buildLog() should use the message property as the message if available. + + +#buildLog() should extend the object with its properties. + + +#log() calls using a pre-built cologne log should maintain the log level. + + +#removeLogger() should do nothing if it can't find a logger. + + +#removeLogger() should remove a logger. + + +#removeLogger() should no longer affect removed logs. + + +#addLogger() should add loggers after instance is live. + + + +ok - test/cologne.js + +# test/utilities.js +::now() should give a precise bigint timestamp. + + +::stringify() should behave like JSON.stringify for non-circular objects. + + +::stringify() should replace circular references with a string. + + +::stringify() should convert bigint to string. + + +::getAnsiCode is sending the correct reset code. + + +::getAnsiCode() should give us a reset code if something weird is sent. + + +::getAnsiCode() should not have duplicated non-reset codes. + + +::getAnsiCode() should not return a reset code in any other supported code. + + +::gettLevelAnsi is red for emerg. + + +::gettLevelAnsi is red for alert. + + +::gettLevelAnsi is red for crit. + + +::gettLevelAnsi is red for error. + + +::gettLevelAnsi is yellow for warn. + + +::gettLevelAnsi is blue for notice. + + +::gettLevelAnsi is blue for info. + + +::gettLevelAnsi is green for debug. + + +::gettLevelAnsi is default for other values. + + + +ok - test/utilities.js + +# test/formatters/simple.js +#format() should output a string in plain mode. + + +#format() should include the from property in plain mode. + + +#format() should include the timestamp property in iso format in plain mode. + + +#format() should include the level string property in plain mode. + + +#format() should include the message property in plain mode. + + +#format() should output a string in color mode. + + +#format() should include the from property in color mode. + + +#format() should include the timestamp property in iso format in color mode. + + +#format() should include the level string property in color mode. + + +#format() should include the message property in color mode. + + +#format() should colorize the string. + + +#format() should colorize only a bit of the string. + + + +ok - test/formatters/simple.js + +# test/formatters/token.js +#format() should output a string in default mode. + + +#format() should include the message in default mode. + + +#format() should output a string in custom mode. + + +#format() with custom string should include the specified tokens (check 1). + + +#format() with custom string should include the specified tokens (check 2). + + +#format() with iso date should include the timestamp as an iso date. + + +#format() should not replace tokens that don't match. + + +#format() should output a string in ansi mode. + + +#format() with ansi tokens should colorize the string. + + +#format() with ansi reset should reset the string. + + +#format() with ansi tokens should colorize the string based on level. + + +#format() should output a string in plain date mode. + + +#format() with plain date should include the timestamp as-is. + + +#format() should output a string in custom search mode. + + +#format() with a custom search, should properly match the new tokens. + + + +ok - test/formatters/token.js + +# test/loggers/file.js +raw file. + + +formatted file. + + +it should send all params to the file. + + +it should log the raw json object. + + +it should send all params to the file. + + +it should log the formatted object. + + + +ok - test/loggers/file.js + +# test/loggers/console.js +It should default to the global console. + + +It should send debug messages to console's #log. + + +It should send info and notice messages to console's #info. + + +It should send warn messages to console's #warn. + + +It should send error messages to console's #error. + + +If available, it should send the objects to the formatter. + + + +ok - test/loggers/console.js +----------------|----------|----------|----------|----------|-------------------| +File | % Stmts | % Branch | % Funcs | % Lines | Uncovered Line #s | +----------------|----------|----------|----------|----------|-------------------| +All files | 100 | 100 | 100 | 100 | | + lib | 100 | 100 | 100 | 100 | | + cologne.js | 100 | 100 | 100 | 100 | | + utilities.js | 100 | 100 | 100 | 100 | | + lib/formatters | 100 | 100 | 100 | 100 | | + simple.js | 100 | 100 | 100 | 100 | | + token.js | 100 | 100 | 100 | 100 | | + lib/loggers | 100 | 100 | 100 | 100 | | + console.js | 100 | 100 | 100 | 100 | | + file.js | 100 | 100 | 100 | 100 | | +----------------|----------|----------|----------|----------|-------------------| diff --git a/doc/README.md b/doc/README.md new file mode 100644 index 0000000..d96cffb --- /dev/null +++ b/doc/README.md @@ -0,0 +1,476 @@ +## Classes + +
+
Cologne
+
+
Utilities
+
+
+ +## Objects + +
+
Formatters : object
+

Namespace that includes the built-in formatters.

+
+
Loggers : object
+

Namespace that includes the built-in loggers.

+
+
+ +## Typedefs + +
+
tCologneLog : object
+

The main cologne log format.

+
+
+ +## Interfaces + +
+
ILogger
+

Main interface for Cologne Loggers

+
+
IFormatter
+

Main interface for Cologne Formatters

+
+
+ + + +## ILogger +Main interface for Cologne Loggers + +**Kind**: global interface + + +### ILogger.log() +Receives any number of cologne log objects and logs them. + +**Kind**: static method of [ILogger](#ILogger) + + +## IFormatter +Main interface for Cologne Formatters + +**Kind**: global interface + + +### IFormatter.format(logObject) ⇒ string +Receives a cologne log object and returns a formatted string. + +**Kind**: static method of [IFormatter](#IFormatter) +**Returns**: string - the formatted log + +| Param | Type | Description | +| --- | --- | --- | +| logObject | [tCologneLog](#tCologneLog) | the log to be formatted | + + + +## Cologne +**Kind**: global class + +* [Cologne](#Cologne) + * [new Cologne()](#new_Cologne_new) + * [.from](#Cologne+from) : String + * [.loggers](#Cologne+loggers) : [Array.<ILogger>](#ILogger) + * [.addLogger(logger)](#Cologne+addLogger) + * [.removeLogger(logger)](#Cologne+removeLogger) ⇒ [Array.<ILogger>](#ILogger) + * [.buildLog(message, [level])](#Cologne+buildLog) ⇒ [tCologneLog](#tCologneLog) + * [.log()](#Cologne+log) + * [.debug()](#Cologne+debug) + * [.info()](#Cologne+info) + * [.notice()](#Cologne+notice) + * [.warn()](#Cologne+warn) + * [.error()](#Cologne+error) + + + +### new Cologne() +The main logger class. It can be instantiated with loggers in order to +send messages to different destinations. + + + +### cologne.from : String +The name of this logger, useful to distinguish between different +loggers. + +**Kind**: instance property of [Cologne](#Cologne) +**Default**: 'Generic Cologne Logger + + +### cologne.loggers : [Array.<ILogger>](#ILogger) +The array containing all the loggers it will call to. + +**Kind**: instance property of [Cologne](#Cologne) +**Default**: [] + + +### cologne.addLogger(logger) +Adds a logger to the current instance. + +**Kind**: instance method of [Cologne](#Cologne) + +| Param | Type | Description | +| --- | --- | --- | +| logger | [ILogger](#ILogger) | the logger to add | + + + +### cologne.removeLogger(logger) ⇒ [Array.<ILogger>](#ILogger) +Removes a logger from the current instance. + +**Kind**: instance method of [Cologne](#Cologne) +**Returns**: [Array.<ILogger>](#ILogger) - the removed log, inside an array. + +| Param | Type | Description | +| --- | --- | --- | +| logger | [ILogger](#ILogger) | the logger to remove | + + + +### cologne.buildLog(message, [level]) ⇒ [tCologneLog](#tCologneLog) +Given a message, it builds a cologne log object without logging it. +If you send a cologne log object, it will only update the level. + +If the message is an object, the log object will be extended with +its properties. + +**Kind**: instance method of [Cologne](#Cologne) +**Returns**: [tCologneLog](#tCologneLog) - a cologne log object + +| Param | Type | Default | Description | +| --- | --- | --- | --- | +| message | \* | | The message to log | +| [level] | number | 6 | The level of the message to log | + + + +### cologne.log() +Default log function. Sends arguments to loggers. If not specified in log +object, it will set the severity to 6 - INFO. + +**Kind**: instance method of [Cologne](#Cologne) + + +### cologne.debug() +Logs with debug level + +**Kind**: instance method of [Cologne](#Cologne) + + +### cologne.info() +Logs with info level + +**Kind**: instance method of [Cologne](#Cologne) + + +### cologne.notice() +Logs with notice level + +**Kind**: instance method of [Cologne](#Cologne) + + +### cologne.warn() +Logs with warn level + +**Kind**: instance method of [Cologne](#Cologne) + + +### cologne.error() +Logs with error level + +**Kind**: instance method of [Cologne](#Cologne) + + +## Utilities +**Kind**: global class + +* [Utilities](#Utilities) + * [new Utilities()](#new_Utilities_new) + * [.now()](#Utilities.now) ⇒ bigint + * [.stringify(object)](#Utilities.stringify) ⇒ String + * [.getAnsiCode(ansiString)](#Utilities.getAnsiCode) ⇒ String + * [.getLevelAnsi(level)](#Utilities.getLevelAnsi) ⇒ String + + + +### new Utilities() +Container object for utilities used by loggers. + + + +### Utilities.now() ⇒ bigint +Returns the current timestamp in nanoseconds as a bigint. + +**Kind**: static method of [Utilities](#Utilities) +**Returns**: bigint - current time in nanoseconds, including fractions. + + +### Utilities.stringify(object) ⇒ String +Stringifies objects, avoiding circular references. + +**Kind**: static method of [Utilities](#Utilities) +**Returns**: String - the stringified object + +| Param | Type | Description | +| --- | --- | --- | +| object | Object | the object to stringify | + + + +### Utilities.getAnsiCode(ansiString) ⇒ String +Given an ansi keyword, it will return the appropriate code. + +**Kind**: static method of [Utilities](#Utilities) +**Returns**: String - the ansi code + +| Param | Type | Description | +| --- | --- | --- | +| ansiString | String | the name of the desired code | + + + +### Utilities.getLevelAnsi(level) ⇒ String +Given a level, it will return the appropriate ansi keyword related +to it. + +**Kind**: static method of [Utilities](#Utilities) +**Returns**: String - the ansi keyword + +| Param | Type | Description | +| --- | --- | --- | +| level | number | the level of the log | + + + +## Formatters : object +Namespace that includes the built-in formatters. + +**Kind**: global namespace + +* [Formatters](#Formatters) : object + * [.Simple](#Formatters.Simple) + * [new Simple()](#new_Formatters.Simple_new) + * [.colorize](#Formatters.Simple+colorize) : Boolean + * [.format(logObjet)](#Formatters.Simple+format) ⇒ String + * [.Token](#Formatters.Token) + * [new Token()](#new_Formatters.Token_new) + * [.formatString](#Formatters.Token+formatString) : String + * [.replaceRule](#Formatters.Token+replaceRule) : RegExp + * [.isoDate](#Formatters.Token+isoDate) : Boolean + * [.format(log)](#Formatters.Token+format) ⇒ String + + + +### Formatters.Simple +**Kind**: static class of [Formatters](#Formatters) +**Implements**: [IFormatter](#IFormatter) + +* [.Simple](#Formatters.Simple) + * [new Simple()](#new_Formatters.Simple_new) + * [.colorize](#Formatters.Simple+colorize) : Boolean + * [.format(logObjet)](#Formatters.Simple+format) ⇒ String + + + +#### new Simple() +Simple formatter. Outputs a predefined format: +`[{{_timestamp}}][{{_levelString}}] {{_from}}: {{message}}`; + + + +#### simple.colorize : Boolean +Flag that tells us whether or not to use ANSI color. Defaults to +false. + +**Kind**: instance property of [Simple](#Formatters.Simple) +**Default**: false + + +#### simple.format(logObjet) ⇒ String +Main entry point, it will read the incoming log object and convert +it to the output string. + +**Kind**: instance method of [Simple](#Formatters.Simple) +**Returns**: String - the formatted object + +| Param | Type | Description | +| --- | --- | --- | +| logObjet | [tCologneLog](#tCologneLog) | the log to format | + + + +### Formatters.Token +**Kind**: static class of [Formatters](#Formatters) +**Implements**: [IFormatter](#IFormatter) + +* [.Token](#Formatters.Token) + * [new Token()](#new_Formatters.Token_new) + * [.formatString](#Formatters.Token+formatString) : String + * [.replaceRule](#Formatters.Token+replaceRule) : RegExp + * [.isoDate](#Formatters.Token+isoDate) : Boolean + * [.format(log)](#Formatters.Token+format) ⇒ String + + + +#### new Token() +Token formatter. Given a format string it will attempt to output +a message. + + + +#### token.formatString : String +The string to use as a template string. By default, any property +inside double curly braces `{{likeThis}}` will be extracted from +the object and replaced. If the object does not contain the +property, it will leave it. + +**Kind**: instance property of [Token](#Formatters.Token) +**Default**: '{{message}}' + + +#### token.replaceRule : RegExp +The regex rule to use to match the tokens. + +**Kind**: instance property of [Token](#Formatters.Token) +**Default**: /{{(.*)}}/g + + +#### token.isoDate : Boolean +Flag that specifies whether or not to use an isoDate when using +`_timestamp`. If false it will output the raw timestamp. + +**Kind**: instance property of [Token](#Formatters.Token) +**Default**: true + + +#### token.format(log) ⇒ String +Main entry point, it will read the incoming log object and convert +all the tokens to their corresponding representation, finally +returning the string. + +**Kind**: instance method of [Token](#Formatters.Token) +**Returns**: String - the formatted object + +| Param | Type | Description | +| --- | --- | --- | +| log | [tCologneLog](#tCologneLog) | the log to format | + + + +## Loggers : object +Namespace that includes the built-in loggers. + +**Kind**: global namespace + +* [Loggers](#Loggers) : object + * [.Console](#Loggers.Console) + * [new Console()](#new_Loggers.Console_new) + * [.console](#Loggers.Console+console) : Object + * [.formatter](#Loggers.Console+formatter) : [IFormatter](#IFormatter) + * [.log()](#Loggers.Console+log) ⇒ undefined + * [.File](#Loggers.File) + * [new File()](#new_Loggers.File_new) + * [.file](#Loggers.File+file) : string + * [.formatter](#Loggers.File+formatter) : [IFormatter](#IFormatter) + * [.log()](#Loggers.File+log) ⇒ undefined + + + +### Loggers.Console +**Kind**: static class of [Loggers](#Loggers) +**Implements**: [ILogger](#ILogger) + +* [.Console](#Loggers.Console) + * [new Console()](#new_Loggers.Console_new) + * [.console](#Loggers.Console+console) : Object + * [.formatter](#Loggers.Console+formatter) : [IFormatter](#IFormatter) + * [.log()](#Loggers.Console+log) ⇒ undefined + + + +#### new Console() +Logger for the javascript console. + + + +#### console.console : Object +The console it will write to, can be any object that looks +and acts like a console, including other cologne objects. + +**Kind**: instance property of [Console](#Loggers.Console) +**Default**: global.console + + +#### console.formatter : [IFormatter](#IFormatter) +The formatter it will use to output the log. If not present it +will output raw JSON + +**Kind**: instance property of [Console](#Loggers.Console) +**Default**: null + + +#### console.log() ⇒ undefined +Main entry point, for each incoming argument it will attempt to +format and send to the console. + +**Kind**: instance method of [Console](#Loggers.Console) + + +### Loggers.File +**Kind**: static class of [Loggers](#Loggers) +**Implements**: [ILogger](#ILogger) + +* [.File](#Loggers.File) + * [new File()](#new_Loggers.File_new) + * [.file](#Loggers.File+file) : string + * [.formatter](#Loggers.File+formatter) : [IFormatter](#IFormatter) + * [.log()](#Loggers.File+log) ⇒ undefined + + + +#### new File() +Logger for files. + + + +#### file.file : string +Path to the file it will write to, must be readable. + +**Kind**: instance property of [File](#Loggers.File) +**Default**: "null" + + +#### file.formatter : [IFormatter](#IFormatter) +The formatter it will use to output the log. If not present it +will output raw JSON + +**Kind**: instance property of [File](#Loggers.File) +**Default**: null + + +#### file.log() ⇒ undefined +Main entry point, for each incoming argument it will attempt to +format and send to the stream to be written. + +**Kind**: instance method of [File](#Loggers.File) + + +## tCologneLog : object +The main cologne log format. + +**Kind**: global typedef +**Properties** + +| Name | Type | Description | +| --- | --- | --- | +| _timestamp | Bigint | the timestamp in nanoseconds | +| _cologneLog | String | main identifier, encodes the version of the cologne log format being used. | +| _from | String | the origin of the log message. | +| _level | String | the severity level of the log, uses syslog priorities. | +| _levelString | String | the severity level keyword of the log, uses syslog priority keywords. | + -- cgit