@jalik/logger
Advanced tools
Comparing version 1.0.5 to 2.0.0
# Changelog | ||
## v2.0.0 | ||
- **BREAKING:** Method `Logger.on(String, Function)` has changed to improve ease of use. Before you had to pass | ||
`debug`, `error`, `info` or `warning` as the first string argument, now use the `log` | ||
event instead. Also in the callback, the `type` of log is inserted as the second argument after `message`, thus `context` is now the third argument. | ||
- BEFORE: `Logger.on('error', (message, context) => {})` | ||
- NOW: `Logger.on('log', (message, type, context) => {})` | ||
- Fixes code examples in README | ||
## v1.0.5 | ||
@@ -4,0 +12,0 @@ - Updates dependencies |
@@ -252,3 +252,3 @@ 'use strict'; | ||
// Notify all listeners | ||
(_observer = this.observer).notify.apply(_observer, [type].concat([message, context])); | ||
(_observer = this.observer).notify.apply(_observer, ['log'].concat([message, type, context])); | ||
} | ||
@@ -255,0 +255,0 @@ |
{ | ||
"name": "@jalik/logger", | ||
"version": "1.0.5", | ||
"version": "2.0.0", | ||
"description": "A logging utility to log messages to anywhere.", | ||
@@ -9,2 +9,3 @@ "license": "MIT", | ||
"error", | ||
"logs", | ||
"logging" | ||
@@ -11,0 +12,0 @@ ], |
@@ -7,5 +7,5 @@ # Logger | ||
Logging is an important part of an application lifecycle, from development to production, we always need to log messages for debugging or tracing errors and warnings. | ||
Logging is an important part of an application lifecycle, from development to production, we always need to log messages for debugging or tracing errors and warnings, this lib will hep you taking control of logging in your apps. | ||
**This library is tested with unit tests.** | ||
**This library has 23 unit tests.** | ||
@@ -17,5 +17,5 @@ ## Creating a logger | ||
```js | ||
import Logger from "@jalik/logger"; | ||
import Logger from '@jalik/logger'; | ||
const Logger = new Logger({ | ||
const logger = new Logger({ | ||
// Activate the logger | ||
@@ -40,7 +40,9 @@ active: true, | ||
Note that `options` are available on each `Logger` instance via `Logger.options`. | ||
Note that after creating the logger, you can still change the options via the public attribute | ||
`options`, like `logger.options.console.debug = false`. | ||
## Logging types | ||
As you can imagine, there are different types of logging, this is useful to distinguish messages, so below are these types : | ||
Instead of levels, this lib refers to types of logging, this is useful to distinguish and filter | ||
messages, there are 4 well known types of logging : | ||
- debug : only used for debugging | ||
@@ -51,11 +53,11 @@ - error : only used for errors and exceptions | ||
You can access predefined logging types by importing them in your code. | ||
You can get the string value of each types by importing the types list. | ||
```js | ||
import Types from "@jalik/logger/dist/types"; | ||
import Types from '@jalik/logger/dist/types'; | ||
Types.debug; | ||
Types.error; | ||
Types.info; | ||
Types.warning; | ||
Types.debug; // used by console.debug() | ||
Types.error; // used by console.error() | ||
Types.info; // used by console.info() | ||
Types.warning; // used by console.warn() | ||
``` | ||
@@ -68,21 +70,26 @@ | ||
```js | ||
import Logger from "@jalik/logger"; | ||
import Logger from '@jalik/logger'; | ||
const Logger = new Logger(); | ||
const logger = new Logger(); | ||
// Logs a debug message | ||
Logger.debug("user json", {name: "karl"}); | ||
// Note: you can use string templates available since ES6 | ||
// to have dynamic logs. | ||
const user = {name: 'karl'}; | ||
logger.debug(`user logged "${user.name}"`, user); | ||
// Logs an error message | ||
Logger.error("Forbidden", {error: new Error("forbidden")}); | ||
Logger.error(new Error("forbidden")); | ||
logger.error('Forbidden', {error: new Error('forbidden')}); | ||
logger.error(new Error('forbidden')); | ||
// Logs an info message | ||
Logger.info("Application started", {date: new Date()}); | ||
logger.info('Application started', {date: new Date()}); | ||
// Logs a warning message | ||
Logger.warn("Disk usage is above 90%", {diskUsage: 92.6}); | ||
logger.warn('Disk usage is above 90%', {diskUsage: 92.6}); | ||
// Logs a custom type message | ||
Logger.log("Plop", "custom-type"); | ||
const ipAddress = '6.6.6.6'; | ||
logger.log(`The IP address ${ipAddress} has failed to login 3 times in one minute`, 'suspicious', | ||
{ipAddress}); | ||
``` | ||
@@ -95,11 +102,11 @@ | ||
```js | ||
import Logger from "@jalik/logger"; | ||
import Logger from '@jalik/logger'; | ||
const Logger = new Logger(); | ||
const logger = new Logger(); | ||
// Activate logger on production environment only | ||
Logger.setActive(process.env.NODE_ENV === "PRODUCTION"); | ||
logger.setActive(process.env.NODE_ENV === 'PRODUCTION'); | ||
// And to check if the logger is active | ||
Logger.isActive(); | ||
logger.isActive(); | ||
``` | ||
@@ -112,9 +119,9 @@ | ||
```js | ||
import Types from "@jalik/logger/dist/types"; | ||
import Logger from "@jalik/logger"; | ||
import Types from '@jalik/logger/dist/types'; | ||
import Logger from '@jalik/logger'; | ||
const Logger = new Logger(); | ||
const logger = new Logger(); | ||
// With this event listener, you can do something when an error happens | ||
Logger.on(Types.error, (message, context) => { | ||
logger.on(Types.error, (message, context) => { | ||
// do whatever you want here... | ||
@@ -125,3 +132,3 @@ // save error to database, send an email... | ||
// This will trigger the listener defined above | ||
Logger.error("Cannot contact DNS server", {ipAddress: "8.8.8.8"}); | ||
logger.error('Cannot contact DNS server', {ipAddress: '8.8.8.8'}); | ||
``` | ||
@@ -131,3 +138,3 @@ | ||
History of releases is in the [changelog](./CHANGELOG.md). | ||
History of releases is in the [changelog](./CHANGELOG.md) on github. | ||
@@ -134,0 +141,0 @@ ## License |
33613
139