yalo.js
Yet Another Logger
const yalo = require("yalo.js")
const logger = yalo.createLogger("output.log")
logger.info("Hello, World!")
logger.error("Error occurred")
Installation
npm install yalo.js
No dependencies required
Usage
// configuration
const options = {
depth: 2,
errors: {
title: "Error",
format: ["=", "-"],
width: 36
}
}
// logger instance
const logger = yalo.createLogger("output.log", options)
// deeply nested object
const object = { a: { b: { c: { d: 0 } } } }
// logs an info message
logger.info(object, "\n")
// removes depth limit
logger.depth = null
// logs full `object`
logger.info(object)
// error() examples
const a = undefined
const b = 0
const c = 36
// options.errors.title
if (!a) logger.error(`a is ${a}`)
// custom title
if (b < 1) logger.error(`b is less than 1`, "Warning")
// dynamic title
if (c > 24) logger.error(`c is more than 24`, () => `c: ${c}`)
logger.info("Hello again!\n")
// used in loops
logger.end()
Output:
{ a: { b: { c: [Object] } } }
{
a: { b: { c: { d: 0 } } }
}
============== Error ==============
a is undefined
-----------------------------------
============= Warning =============
b is less than 1
-----------------------------------
============== c: 36 ==============
c is more than 24
-----------------------------------
Hello again!
Reference
createLogger(dest, options?)
createLogger(
dest: string,
options?: Partial<{
depth: number | null
errors: Partial<{
title: string | Function
format: string[]
width: number
errorMode: boolean
}>
}>
)
dest
- Output file pathoptions
- Logger optionsoptions.depth
-Object
depth limit (default:2
)options.errors
- Error decoration optionsoptions.errors.title
- Error title (default:"Error"
)options.errors.format
- Error decoration format (default:["=", "-"]
)options.errors.width
- Error decoration width (default:80
)options.errors.errorMode
- Error-only streams optimization (default:false
)
@returns
{
fileStream: fs.WriteStream
depth: number | null
errors: {
title: string | Function
format: string[]
width: number
errorMode: boolean
}
info(...data: any[]): void
error(data: any, title?: string | Function): void
end(): void
}
fileStream
: Output streamdepth
:Object
depth limit (options.depth
)errors
: Error decoration options (options.errors
)info()
: Logs info messageserror()
: Logs an error messageend()
: Ends output stream
info(data)
info(...data: any[]): void
data
- Any data
error(data, title?)
error(data: any, title?: string | Function): void
data
- Any datatitle
- Error title (errors.title
)
end()
end(): void
- Ends output stream
- Used in loops
options.errors.errorMode
{
errors: {
errorMode: true
}
}
- Error-only streams optimization (default:
false
) - Removes the last
\n
from the output stream when callingerror()
to save space. - Useful for server error-only log files
Examples
Error logger with Date()
const options = {
errors: {
title: () => new Date(),
errorMode: true
}
}
const logger = yalo.createLogger("error.log", options)
try {
// ...
} catch (error) {
logger.error(error)
}
License
MIT
Made for the Transmoder project