Comparing version 0.2.0 to 0.3.0
# Changelog | ||
### 0.3.0 | ||
- Add `addTrackId` boolean option to `create()` parameter to create a `trackId` in the presets if truthy. | ||
- Improve Readme | ||
### 0.2.0 | ||
@@ -4,0 +9,0 @@ |
@@ -13,3 +13,6 @@ const _ = require('lodash'); | ||
constructor(options) { | ||
const { serviceName, moduleName, presets } = options; | ||
const { | ||
serviceName, moduleName, presets, addTrackId, | ||
} = options; | ||
this.presets = Object.assign( | ||
@@ -19,2 +22,7 @@ { module: moduleName }, | ||
); | ||
if (addTrackId && !this.presets.trackId) { | ||
this.presets.trackId = uuid.v4(); | ||
} | ||
this.debug = debug(`${serviceName}:${moduleName}`); | ||
@@ -21,0 +29,0 @@ this.tag = `${serviceName}-${process.env.NODE_ENV}`; |
@@ -19,3 +19,3 @@ { | ||
"eslint-plugin-security": "1.4.0", | ||
"jest": "23.0.1", | ||
"jest": "23.1.0", | ||
"pre-commit": "1.2.2" | ||
@@ -53,3 +53,3 @@ }, | ||
}, | ||
"version": "0.2.0" | ||
"version": "0.3.0" | ||
} |
@@ -9,3 +9,3 @@ # lalog | ||
``` | ||
```shell | ||
npm i lalog --save | ||
@@ -18,7 +18,6 @@ ``` | ||
If logging to Loggly then these environment variables need to be set: | ||
If logging to Loggly then this environment variable needs to be set: | ||
``` | ||
```javascript | ||
process.env.LOGGLY_TOKEN | ||
process.env.LOGGLY_SUBDOMAIN | ||
``` | ||
@@ -30,12 +29,31 @@ | ||
``` | ||
```javascript | ||
const Logger = require('lalog'); | ||
``` | ||
Create a logger: | ||
### Set the logging level | ||
```javascript | ||
Logger.setLevel('info'); | ||
``` | ||
There are 6 levels: | ||
```javascript | ||
trace, info, warn, error, fatal, security | ||
``` | ||
If you do not call `setLevel(...)` then it will default to `error` and above. | ||
`setLevel` can be used to change the log level at anytime while your app is running and it | ||
will immediately change the level for any loggers that have been created. | ||
### Create a logger | ||
```javascript | ||
const logger = Logger.create({ | ||
serviceName: 'service-name', | ||
moduleName: 'module-name', | ||
presets: {}, // optional | ||
addTrackId: true, // options | ||
}); | ||
@@ -46,15 +64,23 @@ ``` | ||
``` | ||
```javascript | ||
const logger = new Logger({ | ||
serviceName: 'service-name', | ||
moduleName: 'module-name', | ||
presets: {}, // optional | ||
addTrackId: true, // options | ||
}); | ||
``` | ||
`lalog` uses `debug` as one of its destinations. The `serviceName` and `moduleName` props allow | ||
you to filter `debug` messages. | ||
Notes on create: | ||
- `lalog` uses `debug` as one of its destinations. The `serviceName` and `moduleName` props allow | ||
you to filter `debug` messages. A `debug` name of the form `serviceName:moduleName` will be created | ||
which can be used for debugging. | ||
- `presets` is an optional object that will have its contents merged with any object that's logged. Useful for putting in data that you want logged with every message. | ||
- If `addTrackId` is truthy then a `trackId` (uuid) will be added to `presets`. | ||
- The `moduleName` is added to `presets` as `module`. | ||
### Write Log Messages | ||
``` | ||
```javascript | ||
logger.trace({ | ||
@@ -65,2 +91,52 @@ message: 'I am a message' | ||
There are 6 levels at which to write log messages: | ||
```javascript | ||
trace, info, warn, error, fatal, security | ||
``` | ||
The log will only be written to the destination if the log level has be set at this or above. | ||
It defaults to `error` if not set. | ||
The only parameter you can pass to `logger.<level>()` is an object which will be written to | ||
the destination after some modifications have been made to it: | ||
- If the log is `error` or above then the object will be written to the destination. | ||
### time(label) and timeEnd(label, [logObject]) | ||
`time()` and `timeEnd()` work pretty much the same as `console.time()` and `console.timeEnd()`. | ||
`time('label')` starts the timer and `timeEnd('label')` stops the timer and writes the log. | ||
`timeEnd()` operates at the `info` level. i.e. it will only write to the destination if the level | ||
is set to `info` or `trace`. | ||
`timeEnd` has the same modifiers that a created logger has inasmuch as it can be called as: | ||
```javascript | ||
time('label'); | ||
// do some stuff | ||
timeEnd.warn('label'); | ||
// or | ||
timeEnd.error('label'); | ||
``` | ||
`timeEnd()` and `timeEnd.<level>()` take an optional log object. | ||
This allows you to do: | ||
```javascript | ||
try { | ||
logger.time('write-to-db'); | ||
await writeToDb(); | ||
logger.timeEnd('write-to-db'); | ||
} catch(err) { | ||
logger.timeEnd.error('write-to-db', { err }); | ||
} | ||
``` | ||
This saves you from having to do a `logger.error()` and a `logger.timeEnd()` if an error is caught. | ||
Also if the level is set to error then the timing will be captured in the event of an error | ||
(in addition to any extra logging data) but not in the event of normal operation because the | ||
default level for `logger.timeEnd()` is `info`. |
@@ -96,9 +96,20 @@ const Logger = require('../../lib'); | ||
test('should create a logger with presets', () => { | ||
const logger1 = new Logger({ | ||
const testLogger = new Logger({ | ||
serviceName: 'fake-service-1', | ||
moduleName: 'fake-module-1', | ||
presets: { trackId: 'fake-track-id' }, | ||
presets: { testProp: 'fake-track-id' }, | ||
}); | ||
expect(typeof logger1).toBe('object'); | ||
expect(typeof testLogger).toBe('object'); | ||
expect(testLogger.presets.testProp).toBe('fake-track-id'); | ||
}); | ||
test('should create a presets trackId with addTrackId option', () => { | ||
const testLogger = new Logger({ | ||
serviceName: 'fake-service-1', | ||
moduleName: 'fake-module-1', | ||
addTrackId: true, | ||
}); | ||
expect(typeof testLogger).toBe('object'); | ||
expect(testLogger.presets.trackId).toHaveLength(36); | ||
}); | ||
}); |
Sorry, the diff of this file is not supported yet
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
27593
612
138