logee v1.0.1
Logee
A simple logger utility.
Install
Require Node.js >=12.0
.
npm i logee
Usage
This package is pure ESM, please read the esm-package.
import { info, success, warn, error } from 'logee'
info('Hello, world!')
// Prints "ℹ Hello, world!"
success('This is a success!')
// Prints "✔ This is a success!"
warn('This is a warning!')
// Prints "⚠ This is a warning!"
error('Something went wrong!')
// Prints "✖ Something went wrong!"
By default the above example will print messages to the console in all environments, except for
test
environment. Please seesetLogEnv()
for how to change this behavior.
Debuggable messages can be printed with debug
:
import { debug } from 'logee'
// Debug the duration of an operation
const expensiveFn = () => {
// doing things 100 times
for (let i = 0; i < 100; i++) {}
return 'done'
}
debug(expensiveFn)
// Prints "debugging expensiveFn: 0.097ms"
// Debug the duration of an async operation
const expensiveAsyncFn = async () => {
// doing things 100 times
for (let i = 0; i < 100; i++) {}
return 'done'
}
await debug(expensiveAsyncFn)
// Prints "debugging expensiveAsyncFn: 0.056ms"
The above example will print debuggable messages to the console in all environments, by default. Please see
setDebugEnv()
for how to change this behavior.
API
▸ print(type
, ...message
): void
Logs the message
for the given type
to the console if current environment is resolved.
import { print } from 'logee'
print('log', 'Hello, world!')
// Prints "Hello, world!"
print('info', 'Hello, world!')
// Prints "Hello, world!"
Parameters
Name | Type |
---|---|
type | LogType |
...message | any[] |
Returns
void
info
▸ info(...message
): void
Logs an info
message to the console.
import { info } from 'logee'
info('Hello, world!')
// Prints "ℹ Hello, world!".
// substitution values
info('count: %d', 5)
// Prints "ℹ count: 5".
Parameters
Name | Type |
---|---|
...message | any[] |
Returns
void
success
▸ success(...message
): void
Logs an success
message to the console.
import { success } from 'logee'
success('Nice, you complete all the tasks.')
// Prints "✔ Nice, you complete all the tasks.".
// substitution values
success('Nice, %s', 'you complete all the tasks.')
Parameters
Name | Type |
---|---|
...message | any[] |
Returns
void
warn
▸ warn(...message
): void
Logs an warn
message to the console.
import { warn } from 'logee'
warn('Wait! You have to check something.')
// Prints "‼ Wait! You have to check something."
// substitution values
warn('Wait! %s', 'You have to check something.')
Parameters
Name | Type |
---|---|
...message | any[] |
Returns
void
error
▸ error(...message
): void
Logs an error
message to the console.
import { error } from 'logee'
error('Oh no! Something went wrong.')
// Prints "✖ Oh no! Something went wrong."
// substitution values
error('Oh no! %s', 'Something went wrong.')
Parameters
Name | Type |
---|---|
...message | any[] |
Returns
void
debug
▸ debug(data
, ...message
): Promise\
This is useful for debugging purposes. By default, It will always logs to the console in all environments, until you set the debugger for specific environments through {@link setDebugEnv}.
import { debug } from 'logee'
debug('Simple debug message.')
// Prints "Simple debug message."
Debug the duration of an operation:
const expensiveFn = () => {
// doing things 100 times
for (let i = 0; i < 100; i++) {}
return 'done'
}
debug(expensiveFn)
// Prints "debugging expensiveFn: 0.097ms"
debug(expensiveFn, 'Custom label')
// Prints "Custom label: 0.097ms"
Debug data in a more readable way:
debug({ foo: 'bar', baz: 'qux' })
// Prints:
// +---------+--------+
// | (index) | values |
// +---------+--------+
// | foo | 'bar' |
// | bar | 'qux' |
// +---------+--------+
debug([
{ a: 1, b: 'Y' },
{ a: 'Z', b: 2 }
])
// Prints:
// +---------+-----+-----+
// | (index) | a | a |
// +---------+-----+-----+
// | 0 | 1 | 'Y' |
// | 1 | 'Z' | 2 |
// +---------+-----+-----+
Parameters
Name | Type |
---|---|
data | any |
...message | any[] |
Returns
Promise\
setLogEnv
▸ setLogEnv(env
): void
Set the env.logger
for the specified environments. If set to true
, it will
log to the console for all environments.
import { setLogEnv } from 'logee'
// Disable logging
setLogEnv(false)
// Enable logging for all environments
setLogEnv(true) // or setLogEnv('*' | ['*'])
// Log to console only when `NODE_ENV` is 'dev' or 'prod'
setLogEnv(['dev', 'prod'])
Parameters
Name | Type |
---|---|
env | LogEnv |
Returns
void
setDebugEnv
▸ setDebugEnv(env
): void
Set the env.debugger
for the specified environments. If set to true
, it will
log to the console for all environments.
import { setDebugEnv } from 'logee'
// Disable debug logging
setDebugEnv(false)
// Enable debug logging for all environments
setDebugEnv(true) // or setDebugEnv('*' | ['*'])
// Debug only when `NODE_ENV` is 'dev' or 'prod'
setDebugEnv(['dev', 'prod'])
Parameters
Name | Type |
---|---|
env | LogEnv |
Returns
void
setLogPrefix
▸ setLogPrefix(text
, type?
): Partial\
Sets prefix text for the log message and returns the changed prefix.
import { setLogPrefix, info, warn } from 'logee'
setLogPrefix('[info]', 'info')
info('Hello, world!')
// Prints "[info] Hello, world!"
// apply to all log types
setLogPrefix('[ProjectName]')
info('Hello, info!')
// Prints "[ProjectName] Hello, info!"
warn('Hello, warn!')
// Prints "[ProjectName] Hello, warn!"
Parameters
Name | Type |
---|---|
text | string |
type? | LogType |
Returns
Partial\
Type aliases
LogType
Ƭ LogType: "log" | "hint" | "info" | "success" | "status" | "details" | "warn" | "warning" | "error" | "fail"
The log message types:
log
hint
info
success
status
(alias ofsuccess
)warn
warning
(alias ofwarn
)details
(alias ofwarn
)error
fail
(alias oferror
)
import { LogType } from 'logee'
LogPrefixMap
Ƭ LogPrefixMap: Record\<LogType, string>
A storage for log prefix message of each log types.
import { LogPrefixMap } from 'logee'
EnvType
Ƭ EnvType: "*" | "dev" | "development" | "prod" | "production" | "test" | "testing" | "ci" | "local" | "continuous-integration" | "continuous-integration-local"
Supported environment types.
import { EnvType } from 'logee'
LogEnv
Ƭ LogEnv: EnvType | EnvType[] | boolean
The Log environment types.
import { LogEnv } from 'logee'