simple-redis-logger
v1.0.0
Published
A simple Node.JS module to store logs into Redis List, based on [node-redis](https://www.npmjs.com/package/redis)
Downloads
2
Readme
Node-Simple-Redis-Logger
About
A simple Node.JS module to store logs into Redis List, based on node-redis
Install
Install via NPM
npm install simple-redis-logger
Then, import it in your project.
let SimpleRedisLogger = require('simple-redis-logger')
Methods
constructor([options])
options
- Optional. An object containing any or all of the following optionsredis
- Optional, a node-redis client instanceconnectOptions
- Optional, a node-redis connection option object, check more details herethrowErrors
- Optional, default isfalse
. IfthrowErrors
is set tofalse
, the module will not throw errors if an error occurs while communicating with Redis server. But it will still throw Errors if an error occurs ingetLogs
method. Anderror
event will always emit while an error occurs.limitAmount
- Optional, the amount of log messages in every list(or calledtypes
in this case). Default isnull
, which means there is no limit on the amount of messages.serviceName
- Optional, the prefix of the list. Default issimple-logger
.
redis
orconnectOptions
is Required.
log(type)(content)
type
- type of the message.content
- Can be string, Object, or Error Object.- String: The module will store the original string to the Redis List
- Object: Using
JSON.stringify
to convert the Object into String, then store to Redis List - Error Object: Module will store
myError.message
to the Redis List
[async] getLogs(type[, from[, end]])
type
- type of the messagesfrom
- Optional, the beginning index of the messages you want to get. Default is0
end
- Optional, the ending index of the messages you want to get. Default is-1
Returns
logs
- An array of the logslog
- A log Objectcontent
- The log contentstimestamp
- The timestamp of the time when the log is committed.time
- A Date Object of the time when the log is committed.
Example
To create a logger
let logger = new SimpleRedisLogger({
serviceName: 'test_logger',
limitAmount: 3
})
logger.log('info')(new Error('This is an Error'))
logger.log('info')('This is an example of String message')
logger.log('info')({
message: 'This is an example of Object'
})
To get logs
logger.getLogs('info')
.then((infos) => {
...
})