1.2.0 • Published 6 years ago

node-db-logger v1.2.0

Weekly downloads
4
License
mit
Repository
github
Last release
6 years ago

node-db-logger

Node Database Logger

GitHub package.json version GitHub GitHub All Releases GitHub code size in bytes

A simple database logger.

  • supported databases: MongoDB

All logs are saved into single collection having following structure.

propertydescription
timelog timestamp
identifieridentifier
typelog type
datalog data

Installation

npm install node-db-logger --save

How to use

Create logger

  • config: configuration object, or string to get reusable logger
  • identifier: string, not required - will create a reusable logger if set and will point every record in the collection to this identifier
const logger = require('node-db-logger').createLogger({
  // config 
  mongo: {
    url     : 'mongodb://localhost:27017',
    options : {
      useUnifiedTopology: true
    },
    database: 'demo',
    collection: 'demoLogs'
  }
})

// create reusable logger
require('node-db-logger').createLogger({
  // config 
  mongo: {
    url     : 'mongodb://localhost:27017',
    options : {
      useUnifiedTopology: true
    },
    database: 'demo',
    collection: 'demoLogs'
  }
},'identifier') 

// use it everywhere
const reusableLogger = require('node-db-logger').createLogger('identifier')

record(type, ...data)

Create a log record

  • type: the value of this parameter will go into the type field of the document
  • data: data to be records
const test = 'test'
logger.record('info', 'some ', test, ' string')
logger.record('error', 1)
logger.record('warn', true)
logger.record('debug', {test: 'test'})
logger.record('trace', [1, 2, 3])
logger.record('ClassName::lineNumber or something else', [1, 2, 3], [4, 5, 6], [7, 8, 9])  

delete(...types)

Delete the log records.
In use with unidentified logger will delete only unidentified records (identifier = NULL).
In use with identified reusable logger will delete only the identified records for current identifier.

  • types: types to be deleted, will delete all not set
// Delete all unidentified records or identified records of current identifier 
logger.delete() 

// Delete all unidentified records or identified records of current identifier of "some type" and "error" 
const type = "some type" 
logger.delete(type, 'error')

forceDelete(...types)

Same as delete but ignoring the identifier rules.

instance.identifier

To get the identifier of this logger.

Config

configrequireddefaultdescription
mongo.urltrueConnection url
mongo.optionsfalse{}Mongo Options
mongo.databasetrueDatabase name
mongo.collectionfalselogsCollection name
1.2.0

6 years ago

1.1.1

6 years ago

1.1.2

6 years ago

1.0.1

6 years ago

1.0.0

6 years ago