@kkg/logger
v2.3.0
Published
Logger Wrapper using Winston for Node.js
Downloads
52
Readme
Logger
A Powerful Logger module Made by @rentspree
Features
- Winston Logging
- Google Stackdriver Logging
- Elasticsearch Logging
Install
npm install --save @rentspree/logger# Winston Logging Usage
import createLogger from "@rentspree/logger"
const logger = createLogger({ logLevel: "info" })
logger.info("hello info")
logger.info("hello %s", "hola")
logger.info("hello %s %s", "hola", "people")
const obj = { _id: "21344325", name: "John Doe" }
logger.info(obj)
logger.error("user Object", obj)
logger.debug("should not log this message")Log Levels Options
- default
- silly
- verbose
- debug
- info
- warn
- error
Log Format Options
text-defaultjson
Logging with JSON Format
import createLogger from "@rentspree/logger"
const logger = createLogger({ logLevel: "info", logFormat: "json" })
const obj = { _id: "21344325", name: "John Doe" }
logger.info(obj)
logger.error("user Object", obj)Production Environment
Production will output with JSON format by default for support stackdriver
NODE_ENV=production# Google Stackdriver Logging Usage
Alternative logging for store log from our application to Stackdriver logging
import createStackdriverLogger from "@rentspree/logger/dist/stackdriver"
// or
import { createStackdriverLogger } from "@rentspree/logger"
const credentials = JSON.parse(
fs.readFileSync(path.join(__dirname, "../private-key/logging-key.json")),
)
const logger = createStackdriverLogger({ credentials, logName = "example" })
logger.error("error level")
logger.warn("warning level")
logger.warning("warning level")
logger.info("info level")
logger.debug("debug level")
logger.notice("notice level")
logger.critical("critical level")
logger.alert("alert level")
logger.emergency("emergency level")
// ** Support only string and object and only one param **
logger.info("text log message")
logger.info({ _id: "21344325", name: "John Doe" })Log Levels Options
- error
- warning
- info
- debug
- notice
- critical
- alert
- emergency
Log Format Options
json-support only json
# Elasticsearch Logging Usage
Alternative logging for store log from our application to ELK Stack. Powered by https://github.com/vanthome/winston-elasticsearch
Elasticsearch Mapping
Please ensure that in Elasticsearch has *app_logs-* mapping pattern to be ensured as parsed JSON.
if not you can copy from mapping/message.json folder
Available Options
These are default options you can override value by passing an option when initialize createElasticsearchLogger instance
{
logLevel: "info",
esHost: "http://localhost:9200",
esVersion: 7,
esEnvPrefix: "dev",
esIndexPrefix: "app_logs",
esIndexSuffixPattern: "YYYY-MM",
esRequestTimeout: 10000,
esMaxRetries: 1,
}Example
import createElasticsearchLogger from "@rentspree/logger/dist/elasticsearch"
// or
import { createElasticsearchLogger } from "@rentspree/logger"
const logger = createElasticsearchLogger({
logLevel: "info",
})
logger.info("test", {})
logger.info({firstName:'John',lastName:'snow'})
logger.info("info message")
const obj = { _id: "21344325", name: "John Doe" }
logger.error("user object message", obj)
logger.debug("should not log this message")
// circular structure
const o = { b: 1, a: 0 }
o.o = o
logger.info("test circular structure", o)
// the global logger name can be set via `setName`
logger.setName("rentspree-auth-service-v2")
// can create a new logger bind to the metadata via createMeta
const controllerLogger = logger.createMeta("controller.js")
controllerLogger.info("this is log with metadata to controller")
const externalServiceLogger = logger.createMeta("external-service.js")
externalServiceLogger.info("this is log with metadata to external-service")
Log Levels Options
- silly
- verbose
- debug
- info
- warn
- error
License
MIT © RentSpree
