0.3.1 • Published 6 years ago
@robb_j/chowchow-logger v0.3.1
Chow Chow | Logger
Provides logging to chowchow using winston with npm logging levels.
// An example endpoint using the logger
export async function listProducts({ req, res, logger }: Context) {
if (!req.query.token) {
logger.error('Unauthenticated session')
} else {
logger.info('Authenticated', { token: req.query.token })
}
try {
let products = await fetchProducts()
logger.debug(`Found ${products.length} product(s)`)
res.send({ products })
} catch (error) {
logger.error(error.message)
res.status(500).send({ msg: 'Something went wrong' })
}
}Here is how you set it up:
import { ChowChow, BaseContext } from '@robb_j/chowchow'
import { LoggerModule, LoggerContext } from '@robb_j/chowchow-logger'
type Context = BaseContext & LoggerContext
;(async () => {
let chow = ChowChow.create<Context>()
chow.use(new LoggerModule({ path: 'path/to/logs' }))
await chow.start()
})()Features
- Adds
loggerto the context, awinston.Loggerinstance- Save logs to the path specified by
pathoption - Configure what is logged with
LOG_LEVELenv variable - Logs are written to different files in your log folder
- Save logs to the path specified by
- Optionally log http requests by passing
enableAccessLogs- Exclude routes using
excludeRoutesparameter, tested againstreq.path
- Exclude routes using
- Optionally log errors from http requests by passing
enableErrorLogs - Has different levels of logging, using winston's npm levels
- Configure which levels are written to files in your log folder by passing
persistentLevels- Defaults to write
error,warn,infoanddebugto their own files - Following
winston, logs levels are inherited so higher levels are included in the lower ones. E.g.infologs will include allwarnanderrormessages.
- Defaults to write
Environment variables
Set the LOG_LEVEL environment variable to configure which log levels are processed.
For example, if it is set to error, only error messages will be logged and written to files.
All logs at and under the level you set will be logged, so if you want everything set it to silly.
For more see: npm logging levels
Configuration
There is one required constructor parameter:
path: string- This is the path to a folder where your logs will be stored. The folder will be created if it doesn't exist.
There are also option parameters to enabled additional features:
enableAccessLogs: boolean– Turn on http traffic logging (default:false)excludeRoutes: RegExp[]– An array of RegExps to match paths you don't want to log (default:[])enableErrorLogs: boolean– Turn on error logging, logs errors caught inside endpoints (default:false)persistentLevels: string[]– Which log levels you want to be written to files (default:error, warn, info, debug)
Dev Commands
# Run the app in dev mode
npm run dev:once
# Run in dev mode and restart on file changes
npm run dev:watch
# Lint the source code
npm run lint
# Manually format code
# -> This repo runs prettier on git-stage, so committed code is always formatted
npm run prettier
# Run the unit tests
npm test
# Generate code coverage in coverage/
npm run coverage