Comparing version 1.7.1 to 1.7.2
@@ -52,10 +52,14 @@ 'use strict'; | ||
var logLevel = message.context.logLevel.toUpperCase(); | ||
var formattedMessage = ''; | ||
var logLevelColorName = logLevelColorMap[logLevel] || 'inverse'; | ||
formattedMessage = '[' + new Date(message.time).toISOString() + ']'; | ||
var formattedMessage = ''; | ||
if (message.context.logLevel) { | ||
var logLevel = message.context.logLevel.toUpperCase(); | ||
formattedMessage = '[' + new Date(message.time).toISOString() + '] ' + _chalk2.default[logLevelColorName](logLevel); | ||
var logLevelColorName = logLevelColorMap[logLevel] || 'inverse'; | ||
formattedMessage += ' ' + _chalk2.default[logLevelColorName](logLevel); | ||
} | ||
if (message.context.package) { | ||
@@ -62,0 +66,0 @@ formattedMessage += ' (@' + message.context.package + ')'; |
@@ -71,2 +71,3 @@ 'use strict'; | ||
var _loop = function _loop(logLevel) { | ||
// eslint-disable-next-line id-length | ||
log[logLevel] = function (a, b, c, d, e, f, g, h, i, k) { | ||
@@ -73,0 +74,0 @@ return log.child({ |
@@ -77,3 +77,3 @@ { | ||
}, | ||
"version": "1.7.1" | ||
"version": "1.7.2" | ||
} |
@@ -31,2 +31,6 @@ # Roarr | ||
* [Using Roarr in an application](#using-roarr-in-an-application) | ||
* [Using Roarr in modules](#using-roarr-in-modules) | ||
* [Recipes](#recipes) | ||
* [Logging errors](#logging-errors) | ||
* [Using with Elasticsearch](#using-with-elasticsearch) | ||
@@ -374,2 +378,4 @@ ## Motivation | ||
To avoid code duplication, you can use a singleton pattern to export a logger instance with predefined context properties (e.g. describing the application). | ||
I recommend to create a file `Logger.js` in the project directory. Use this file to create an child instance of Roarr with context parameters describing the project and the initialisation instance, e.g. | ||
@@ -412,1 +418,85 @@ | ||
``` | ||
### Using Roarr in modules | ||
If you are developing a code that is designed to be consumed by other applications/ modules, then you should avoid using global.ROARR (though, there are valid use cases). However, you should still start the project by defining a `Logger.js` file and use log.child instead. | ||
```js | ||
/** | ||
* @file Example contents of a Logger.js file. | ||
*/ | ||
import Roarr from 'roarr'; | ||
export default Roarr.child({ | ||
domain: 'database', | ||
package: 'my-package' | ||
}); | ||
``` | ||
Roarr does not have reserved context property names. However, I encourage use of the conventions. The `roarr pretty-print` [CLI program](#cli-program) is using the context property names suggested in the [conventions](#conventions) to pretty-print the logs for the developer inspection purposes. | ||
## Recipes | ||
### Logging errors | ||
This is not specific to Roarr – this suggestion applies to any kind of logging. | ||
If you want to include an instance of [`Error`](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Error) in the context, you must serialize the error. | ||
The least-error prone way to do this is to use an existing library, e.g. [`serialize-error`](https://www.npmjs.com/package/serialize-error). | ||
Without using serialisation, your errors will be logged without the error name and stack trace. | ||
### Using with Elasticsearch | ||
If you are using [Elasticsearch](https://www.elastic.co/products/elasticsearch), you will want to create an [index template](https://www.elastic.co/guide/en/elasticsearch/reference/current/indices-templates.html). | ||
The following serves as the ground work for the index template. It includes the main Roarr log message properties (context, message, time) and the context properties suggested in the [conventions](#conventions). | ||
```json | ||
{ | ||
"mappings": { | ||
"log_message": { | ||
"_source": { | ||
"enabled": true | ||
}, | ||
"dynamic": "strict", | ||
"properties": { | ||
"context": { | ||
"dynamic": true, | ||
"properties": { | ||
"application": { | ||
"type": "keyword" | ||
}, | ||
"hostname": { | ||
"type": "keyword" | ||
}, | ||
"instanceId": { | ||
"type": "keyword" | ||
}, | ||
"logLevel": { | ||
"type": "keyword" | ||
}, | ||
"namespace": { | ||
"type": "text" | ||
}, | ||
"package": { | ||
"type": "text" | ||
} | ||
} | ||
}, | ||
"message": { | ||
"type": "text" | ||
}, | ||
"time": { | ||
"format": "epoch_millis", | ||
"type": "date" | ||
} | ||
} | ||
} | ||
}, | ||
"template": "logstash-*" | ||
} | ||
``` |
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
59197
361
500