Log messages in the terminal and browser
This is a small, but useful, library for logging messages in the terminal and browser consoles.
Colorful messages can be logged in the terminal and browser consoles that support ANSI colors.
👌 Easy to use
🔌 Pluggable reporters
💻 Consistent terminal experience
🔖 Tag support
🌐 Browser support
This package is ESM only.
In Node.js with yarn:
yarn add @flex-development/log
See Git - Protocols | Yarn for details regarding installing from Git.
In Deno with esm.sh
:
import { createLogger } from 'https://esm.sh/@flex-development/log'
In browsers with esm.sh
:
<script type="module">
import { logger } from 'https://esm.sh/@flex-development/log'
</script>
With bun:
bun add @flex-development/log
See bun add
for more details.
import { logger } from '@flex-development/log'
logger.info('Using @flex-development/log 5.0.0')
logger.start('Building project...')
logger.warn('A new version of @flex-development/log is available: 5.0.1')
logger.success('Project built!')
logger.fail(new Error('This is an example error. Everything is fine!'))
Will display in the terminal:
This package exports the following identifiers:
The default export is logger
.
(Logger
) The default, pre-configured logger.
Logs are written using the FancyReporter
.
Create a new logger.
options
(LogLevelOption
|LoggerOptions
, optional) — Log level or configuration options
(Logger
) Logger object
(Readonly<Record<LogType, LogLevel>>
) Map where each key is a log type
and each value is a log level.
Log reporter (abstract class
).
logger
(Logger
) — the loggerthis
reporter writes to
Initialize the reporter.
logger
(Logger
) — the loggerthis
reporter writes to
(this
) this
reporter
Define how a log message is processed and displayed by this
reporter.
info
(LogObject
) — the log information to process
(undefined | void
) Nothing.
Log reporter with basic utilities (abstract class
).
Fancy log reporter (class
).
This package is fully typed with TypeScript.
Logger API (TypeScript interface).
browser
(boolean
,readonly
) — whether the logger is operating in a browser environmentget color(): boolean
— whether color logs are enabledset color(color: boolean | null | undefined)
— enable or disable color log. color will be disabled if not supportedcolor
(boolean | null | undefined
) — color logs enabled?
get colors():
Colors
— get a colorizer based on the currentcolor
configurationcreate
(Create
) — create a new logger, inheriting options from the current instance, with possible overridesdefaults
(InputLogObject
) — properties to apply to all logs, regardless of log type or leveleol
(string
) — the character, or characters, used to signify the end of a lineformat
(LogFormatOptions
) — formatting optionsget level():
LogLevel
— get the current log levelset level(level: LogLevelOption | null | undefined)
— set the maximum log level to outputlevel
(LogLevelOption
|null
|undefined
) — maximum log level (inclusive)
levels
(Readonly<LogLevelMap>
,readonly
) — log level mapreporters
(Set<Reporter>
,readonly
) — list of reporter instances used to handle and output log messagesstderr
(WriteStream
) — the writeable stream for standard error outputstdout
(WriteStream
) — the writeable stream for standard outputtypes
(Record<LogType, InputLogObject>
) — record, where each key is aLogType
and each value is anInputLogObject
defining the configuration for the log typeunicode
(boolean
) — whether unicode is supportedwithDefaults
(WithDefaults
) — create a new logger with the specified default log object propertieswithTag
(WithTag
) — create a new logger with the specified tag. the tag will be included in any logs sent from the new logger
Create a new logger, inheriting options from the current instance, with possible overrides (TypeScript interface).
Plain objects (i.e. options.format
, options.types
) are merged recursively.
options
(LoggerOptions
, optional) — overrides for the new logger
(Logger
) The new logger.
Input log data object (TypeScript interface).
additional?
(string | string[]
, optional) — an additional line, or list of lines, to be logged with the messageargs?
(unknown[]
, optional) — format argumentscolor?
(Color
, optional) — color associated with the logdate?
(Date
, optional) — timestampformat?
(LogFormatOptions
, optional) — format optionsicon?
(string
, optional) — icon to displaylevel?
(LogLevelOption
, optional) — log levelmessage?
(unknown
, optional) — log message; inserted intoargs
as the first format argument if definedstack?
(string
, optional) — stack tracetag?
(string
, optional) — a string to categorize or identify the logtype?
(LogType
, optional) — log type
Use util.inspect
on value
and print its string representation (TypeScript type).
value
(unknown
) — the thing to inspectoptions
(InspectOptions
, optional) — inspection options
(undefined
) Nothing.
Options for inspecting a value (TypeScript interface).
colors
(boolean
, optional) — whether to use color
Log formatting options (TypeScript interface).
badge?
(boolean
, optional) — whether to display the log type as a badgecolumns?
(number
, optional) — the maximum number of columns to outputdate?
(boolean
, optional) — whether to include timestamp information in log messagesicon?
(boolean
, optional) — whether to display the icon associated with the log
Send a message to all reporter instances (TypeScript interface).
(message: InputLogObject | string, ...args: unknown[]) => undefined | void
(message: unknown, ...args: unknown[]) => undefined | void
message
(InputLogObject
|unknown
) — the message to write...args
(unknown[]
, optional) — message arguments
(undefined | void
) Nothing.
Log formatting options (TypeScript interface).
inspect
(Inspect
) — useutil.inspect
on a value and print its string representation
Union of log levels (TypeScript type).
To register custom log levels, augment LogLevelMap
.
They will be added to the union automatically.
type LogLevel = LogLevelMap[keyof LogLevelMap]
Registry of log levels (TypeScript interface).
interface LogLevelMap {/* see code */}
When developing extensions that use additional levels, augment LogLevelMap
to register custom log levels:
declare module '@flex-development/log' {
interface LogLevelMap {
box: 3
}
}
Union of log level options (TypeScript type).
type LogLevelOption = LogLevel | LogLevelType
Union of log level types (TypeScript type).
To register custom log level types, augment LogLevelMap
.
They will be added to the union automatically.
type LogLevelType = Extract<keyof LogLevelMap, string>
Log data object (TypeScript interface).
additional?
(string[]
, optional) — additional lines to be logged with the messageargs
(unknown[]
) — format argumentsdate
(Date
) — timestamplevel
(LogLevel
) — log levelmessage?
(null | undefined
, optional) — log messagetype
(LogType
) — log type
Union of log types (TypeScript type).
To register custom log types, augment LogTypeMap
.
They will be added to the union automatically.
type LogType = LogTypeMap[keyof LogTypeMap]
Dictionary of log type functions (TypeScript type).
To register custom log type functions, augment LogTypeMap
.
They will be added to the union automatically.
type LogTypeFunctions = { [T in LogType]: LogFunction }
Registry of log types (TypeScript interface).
interface LogTypeMap {/* see code */}
When developing extensions that use additional types, augment LogTypeMap
to register custom log types:
declare module '@flex-development/log' {
interface LogTypeMap {
box: 'box'
}
}
Logger configuration options (TypeScript interface).
defaults?
(InputLogObject
, optional) — properties to apply to all logs, regardless of log type or level. defaults can be overridden per log type usingtypes
eol?
(string
, optional) — the character, or characters, used to signify the end of a linecolor?
(Color
) — color associated with the logformat?
(LogFormatOptions
, optional) — formatting optionslevel?
(LogLevelOption
, optional) — the maximum log level to outputreporters?
(ReportersOption
, optional) — reporter instances used to handle and output log messagesstderr?
(WriteStream
, optional) — the writeable stream for standard error outputstdout?
(WriteStream
, optional) — the writeable stream for standard outputtypes?
(Partial<Record<LogType, InputLogObject>>
, optional) — record, where each key is aLogType
and each value is anInputLogObject
defining the configuration for the log type
Union of values used to configure reporters (TypeScript type).
type ReportersOption =
| Reporter
| Set<Reporter | false | null | undefined>
| readonly (Reporter | false | null | undefined)[]
Write data to the stream (TypeScript type).
buffer
(string
) — the data to write
(boolean | undefined | void
) true
if all data was flushed successfully, false
if all or part of the data was
queued in user memory, or nothing.
Create a new logger with the specified default log object properties (TypeScript interface).
defaults
(InputLogObject
, optional) — default properties to apply to any log reported from the new logger
(Logger
) The new logger.
Create a new logger with the specified tag
(TypeScript interface).
tag
(string
) — the tag to include in each log reported from the new loggerseparator
(string
, optional) — the string to used separate tags- default:
':'
- default:
(Logger
) The new logger.
Write stream API (TypeScript interface).
columns?
(number
, optional) — number of columns the tty currently haswrite
(Write
) — write data to the stream
See CONTRIBUTING.md
.
This project has a code of conduct. By interacting with this repository, organization, or community you agree to abide by its terms.