Inspired by NestJS Logger, Logardian was built to output minimalistic, readable logs.
- OTEL Support
- NodeJS v16.17.1
- Logging to file
Note: Logardian with version 3.0.0 or higher requires NodeJS 16+ because of async hooks. If you don't want them downgrade to version 2.1.0
npm i --save logardian
- Various layers of logs that can be turned on/off via config
- OpenTelemetry trace ID support
- In production mode debug() does not work
- Datetime in UTC format
- Format of logs:
[time] [level] [layer] [message]
- In debug mode the path and name of the function that called the log is displayed
- Can be used instead of NestJS Logger
- Can log any objects, arrays, variables
- Modes: normal or json format output
- Colors!
import { Logardian } from 'logardian'
const logger = new Logardian()
logger.configure({
trace: false,
labels: ['users', '*.debug']
})
logger.log(`Hi! I'm info log example`)
logger.warn(`Hi! I'm warn log example`)
logger.error(`Hi! I'm error log example`)
logger.verbose(`Hi! I'm verbose log example`)
logger.debug({ some: 'object' })
logger.markTime('marker')
setTimeout(() => {
logger.measureTime('marker', 'Function take {n} ms to execute')
}, 2000)
logger.log(`I will log`, { label: 'users' })
logger.log(`I will log too`, { label: 'auth.debug' })
logger.log(`I will not log :(`, { label: 'database' })
logger.configure({
json: true
})
{"timestamp":"2022-10-10T12:39:40.012Z","message":"Starting Nest application...","level":"log"}
{"timestamp":"2022-10-10T12:39:40.017Z","message":"AppModule dependencies initialized","level":"log"}
{"timestamp":"2022-10-10T12:39:40.020Z","message":"Nest application successfully started","level":"log"}
{"timestamp":"2022-10-10T12:39:40.022Z","message":"Hi! I'm info log example","level":"log"}
{"timestamp":"2022-10-10T12:39:40.022Z","message":"Hi! I'm warn log example","level":"warn"}
{"timestamp":"2022-10-10T12:39:40.022Z","message":"Hi! I'm error log example","level":"error"}
{"timestamp":"2022-10-10T12:39:40.022Z","message":"Hi! I'm verbose log example","level":"verbose"}
{"timestamp":"2022-10-10T12:39:40.022Z","message":"{\"some\":\"object\"}","level":"debug"}
{"timestamp":"2022-10-10T12:39:40.023Z","message":"I will log","level":"log","label":"users"}
{"timestamp":"2022-10-10T12:39:40.024Z","message":"I will log too","level":"log","label":"auth.debug"}
{"timestamp":"2022-10-10T12:39:42.024Z","message":"Function take 2002.041 ms to execute","level":"timer"}
Labels now support glob patterns! You can dynamically enable and disable the logs you need via logger.configure()
. For example:
import { Logardian } from 'logardian'
const logger = new Logardian()
logger.configure({
labels: ['users.*']
})
logger.log('User sent mail', { label: 'users.email' }) // will log
logger.log('User registered', { label: 'users.auth.registration' }) // will log
logger.log('User authorized', { label: 'users.auth.authorization' }) // will log
logger.log('Database connected', { label: 'database' }) // will NOT log
logger.log('User entity created', { label: 'database.users' }) // will NOT log
Use trace ID from your OTEL
// your create user logic
logger.log('User has been created')
// [2022-10-05 11:34:41.621] [7a22fdae427ddd12ace3a129e344121b] LOG: User has been created
// ^ unique trace id
// your send email for user logic here
logger.log('Email for user was sent')
// [2022-10-05 11:34:47.317] [7a22fdae427ddd12ace3a129e344121b] LOG: Mail for user was sent
// ^ same trace id
const traceId = logger.getTraceId()
// 7a22fdae427ddd12ace3a129e344121b
If you don't like the set of colors logardian provides you can change them in configure()
function.
const logger = new Logardian()
logger.configure({
colors: {
timestamp: '#ABC'
traceId: '#CCC
label: '#FFFFFF
message: '#FFFFFF
trace: '#FFFFFF
stack: '#000000
}
})
NODE_ENV
production start does not show debug() logs
We made logger based on LoggerService but we don't explicitly import it so that we stay dependless of NestJS libraries. But you can also use the Logardian instead of common NestJS logger.
// main.ts
import { Logardian } from 'logardian'
const logger = new Logardian()
async function bootstrap(): Promise<void> {
const app = await NestFactory.create(AppModule, { logger })
await app.listen(port, hostname, () =>
logger.log(`Server running at ${hostname}:${port}`),
)
}
Simply create a new logger class
import { Logardian } from 'logardian'
@Injectable()
export class CatService {
private readonly _logger = new Logardian()
}
Specify labels you want to log or write *
to log every log with label.
Working in production and development mode
Logardian is a singleton, so it means that configure()
works on all Logardian instances
import { Logardian } from 'logardian'
const logger = new Logardian()
logger.configure({
labels: '*', // or ['database', 'events'] or false
trace: false,
json: true,
traceId: true
})
Specify 'false' on logardian config. If you specify trace: true
in logger function trace will log in spite of config option
Priority of trace from high to low:
- Production mode
logger.log('Hello', { trace: true })
logger.configure({ trace: false })