@torgeilo/logger
v0.0.2
Published
Tiny, customizable logger for the browser
Readme
@torgeilo/logger
Tiny, customizable logger, designed for the browser.
Features
- Namespaces.
- Pluggable log handlers.
- Typed and customizable log levels (called tags internally).
- Basic minimal console log handler (default).
- Styled console log handler (separate bundle).
- No dependencies.
Getting started
Install:
npm install @torgeilo/loggerUse:
import { getLogger } from '@torgeilo/logger';
const logger = getLogger('my namespace');
logger.log('Hello');
logger.debug('world');Default console output of the above, with log levels "log" and "debug":
my namespace: Hello
my namespace: worldDefault log levels: debug, log, info, warn, error.
Customizing
Disable logging
Empty the logHandlers array however you prefer. A clear() method is added
for convenience:
import { logHandlers } from '@torgeilo/logger';
logHandlers.clear();Styled console log handler
Import and add the styled console log handler to the logHandlers array.
import { logHandlers } from '@torgeilo/logger';
import { StyledConsoleLogHandler } from '@torgeilo/logger/styled-console-log-handler.js';
logHandlers.clear(); // Remove the default log handler.
logHandlers.push(new StyledConsoleLogHandler());Custom log levels/tags
import { getLogger } from '@torgeilo/logger';
const logger = getLogger<'Finn' | 'Jake'>('Together Again');
logger.Finn('Oh, man, are they angry!');
logger.Jake('Angry and fresh outta ice cream!');Default console output (with level log):
Together Again/Finn: Oh, man, are they angry!
Together Again/Jake: Angry and fresh outta ice cream!Custom log handler
Implement the LogHandler interface and add your implementation to the
logHandlers array. The interface:
export interface LogHandler {
log(namespace: string, tag: string, message: unknown, ...messages: unknown[]): void;
}The tag is typically the log level, which can be checked using isLogLevel():
import { isLogLevel } from '@torgeilo/logger';
const tag = ...;
const message = ...;
if (isLogLevel(tag)) {
console[tag](message);
} else {
console.log(`${tag}:`, message);
}Possibilities
You could:
- Make a log handler which only outputs errors or warnings.
- Make a log handler which shows the log in an HTML element on screen.
- Make a log handler which sends errors to a remote error tracker.
- Make a log handler which sends a custom tag to a remote tracker, like
logger.metric(123);. - Make a test reporter which logs the test run output, and a log handler which sends it somewhere useful, in addition to the console.
License
This software is licensed under the terms of the MIT license.
Contributing
Smaller bug reports are welcome.
I don't have capacity for much else. You're probably better off forking if you want to change things.
