1.0.1 • Published 3 years ago

logee v1.0.1

Weekly downloads
-
License
MIT
Repository
github
Last release
3 years ago

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 see setLogEnv() 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

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

NameType
typeLogType
...messageany[]

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

NameType
...messageany[]

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

NameType
...messageany[]

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

NameType
...messageany[]

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

NameType
...messageany[]

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

NameType
dataany
...messageany[]

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

NameType
envLogEnv

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

NameType
envLogEnv

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

NameType
textstring
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 of success)
  • warn
  • warning (alias of warn)
  • details (alias of warn)
  • error
  • fail (alias of error)
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'
1.0.1

3 years ago

1.0.0

3 years ago