Tiny, customizable logger, designed for the browser.
- 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.
Install:
npm install @torgeilo/loggerUse:
import{getLogger}from'@torgeilo/logger';constlogger=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: world
Default log levels: debug, log, info, warn, error.
Empty the logHandlers array however you prefer. A clear() method is added
for convenience:
import{logHandlers}from'@torgeilo/logger';logHandlers.clear();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(newStyledConsoleLogHandler());import{getLogger}from'@torgeilo/logger';constlogger=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):
TogetherAgain/Finn: Oh,man,aretheyangry!TogetherAgain/Jake: Angryandfreshouttaicecream!Implement the LogHandler interface and add your implementation to the
logHandlers array. The interface:
exportinterfaceLogHandler{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';consttag= ...;constmessage= ...;if(isLogLevel(tag)){console[tag](message);}else{console.log(`${tag}:`,message);}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.
This software is licensed under the terms of the MIT license.
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.