@flex-development/log
v5.0.0
Published
Log messages in the terminal and browser
Maintainers
Readme
:knot: log
Log messages in the terminal and browser
Contents
What is this?
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.
Why this package?
:ok_hand: Easy to use
:electric_plug: Pluggable reporters
:computer: Consistent terminal experience
:bookmark: Tag support
:globe_with_meridians: Browser support
Install
This package is ESM only.
In Node.js with yarn:
yarn add @flex-development/logIn 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/logUse
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:

API
This package exports the following identifiers:
The default export is logger.
logger
(Logger) The default, pre-configured logger.
Logs are written using the FancyReporter.
createLogger([options])
Create a new logger.
Parameters
options(LogLevelOption|LoggerOptions, optional) — Log level or configuration options
Returns
(Logger) Logger object
logLevels
(Readonly<Record<LogType, LogLevel>>) Map where each key is a log type
and each value is a log level.
Reporter
Log reporter (abstract class).
Properties
logger(Logger) — the loggerthisreporter writes to
Methods
init(logger)
Initialize the reporter.
Parameters
logger(Logger) — the loggerthisreporter writes to
Returns
(this) this reporter
abstract report(info)
Define how a log message is processed and displayed by this reporter.
Parameters
info(LogObject) — the log information to process
Returns
(undefined | void) Nothing.
BaseReporter
Log reporter with basic utilities (abstract class).
Extends
FancyReporter
Fancy log reporter (class).
Extends
Types
This package is fully typed with TypeScript.
Logger
Logger API (TypeScript interface).
Extends
Properties
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 currentcolorconfigurationcreate(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 aLogTypeand each value is anInputLogObjectdefining 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
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.
Parameters
options(LoggerOptions, optional) — overrides for the new logger
Returns
(Logger) The new logger.
InputLogObject
Input log data object (TypeScript interface).
Properties
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 intoargsas the first format argument if definedstack?(string, optional) — stack tracetag?(string, optional) — a string to categorize or identify the logtype?(LogType, optional) — log type
Inspect
Use util.inspect on value and print its string representation (TypeScript type).
Parameters
value(unknown) — the thing to inspectoptions(InspectOptions, optional) — inspection options
Returns
(undefined) Nothing.
InspectOptions
Options for inspecting a value (TypeScript interface).
Extends
Properties
colors(boolean, optional) — whether to use color
LogFormatOptions
Log formatting options (TypeScript interface).
Extends
Properties
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
LogFunction
Send a message to all reporter instances (TypeScript interface).
Overloads
(message: InputLogObject | string, ...args: unknown[]) => undefined | void(message: unknown, ...args: unknown[]) => undefined | void
Parameters
message(InputLogObject|unknown) — the message to write...args(unknown[], optional) — message arguments
Returns
(undefined | void) Nothing.
LogFunctions
Log formatting options (TypeScript interface).
Extends
Properties
inspect(Inspect) — useutil.inspecton a value and print its string representation
LogLevel
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]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
}
}LogLevelOption
Union of log level options (TypeScript type).
type LogLevelOption = LogLevel | LogLevelTypeLogLevelType
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>LogObject
Log data object (TypeScript interface).
Extends
Properties
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
LogType
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]LogTypeFunctions
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 }LogTypeMap
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'
}
}LoggerOptions
Logger configuration options (TypeScript interface).
Properties
defaults?(InputLogObject, optional) — properties to apply to all logs, regardless of log type or level. defaults can be overridden per log type usingtypeseol?(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 aLogTypeand each value is anInputLogObjectdefining the configuration for the log type
ReportersOption
Union of values used to configure reporters (TypeScript type).
type ReportersOption =
| Reporter
| Set<Reporter | false | null | undefined>
| readonly (Reporter | false | null | undefined)[]Write
Write data to the stream (TypeScript type).
Parameters
buffer(string) — the data to write
Returns
(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.
WithDefaults
Create a new logger with the specified default log object properties (TypeScript interface).
Parameters
defaults(InputLogObject, optional) — default properties to apply to any log reported from the new logger
Returns
(Logger) The new logger.
WithTag
Create a new logger with the specified tag (TypeScript interface).
Parameters
tag(string) — the tag to include in each log reported from the new loggerseparator(string, optional) — the string to used separate tags- default:
':'
- default:
Returns
(Logger) The new logger.
WriteStream
Write stream API (TypeScript interface).
Properties
columns?(number, optional) — number of columns the tty currently haswrite(Write) — write data to the stream
Contribute
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.
