- Notifications
You must be signed in to change notification settings - Fork1
Lightweight TypeScript logger with flexible custom transports.
License
toreda/log
Folders and files
Name | Name | Last commit message | Last commit date | |
---|---|---|---|---|
Repository files navigation
Light TypeScript logger for node, web, and serverless environments.
Features:
- Small footprint
- Simple to use
- Fully supports TypeScript
- Custom Transport support
- Works in Browser, Serverless, and Node environments.
- Configure transports to receive all log events, or only a filtered subset based on class, group, and log level.
- Custom transports can filter and receive structured log data for specific events you care about. Get the exact functionality you need without writing a whole library.
- Leave disabled log messages in prod environments which can be turned on later for debugging without a code push.
- Set log levels for individual functions, classes, and groups. See debug output from the system you're debugging without seeing app-wide debug spam.
@toreda/log
provides simple and straight forward logging for common use cases, and advanced functionality for use in more complicated situations like server-side and remote debugging.
Create Logger
import{Log}from'@toreda/log';constlog=newLog();
Log Levels
import{Log,Levels}from'@toreda/log';constlog=newLog({globalLevel:Levels.DEBUG});// Change log level:log.setGlobalLevel(Levels.ALL);// Disable a specific log level onlylog.disableGlobalLevel(Levels.TRACE)// Disable specific log levels onlylog.disableGlobalLevels([Levels.DEBUG,Levels.INFO])// Enable a specific log level onlylog.enableGlobalLevel(Levels.DEBUG)// Enable specific log levels onlylog.enableGlobalLevels([Levels.TRACE,Levels.INFO])// Tracelog.trace('Trace message here');// Debuglog.debug('Debug message here');// Infolog.info('Info message here');// Warnlog.warn('Warn message here');// Errorlog.error('my','message','here');// Multplelog.log(Levels.ERROR&Levels.TRACE,'trace and error message here');// CustomconstcustomLevel=log.log(0b0010_0000_0000,'custom logging level');
GroupsNew logs can be created from existing logs. The new logs share a state withthe log that created them and have an id that tracks the origin of of the log.
import{Log}from'@toreda/log';constlog=newLog({id:'ClassLog'});constsubLog=log.makeLog('FunctionLog');// Message has id 'ClassLog'log.info('Class constructor started.');// Message has id 'ClassLog.FunctionLog'subLog.info('Function call started.');log.globalState===subLog.globalState;// returns true
TransportsTransports attach to logs and handle the messages.
A default transport that logs to console can be actived when creating the log.
import{Log}from'@toreda/log';constlog=newLog({consoleEnabled:true});constsublog=log.makeLog('sublog');// Logs to the consolelog.info('Info Message');// Logs to the consolesublog.info('Info Message');
It can also be activated later
import{Log}from'@toreda/log';constlog=newLog();log.activateDefaultConsole();constsublog=log.makeLog('sublog');// Logs to the consolelog.info('Info Message');// Does not log to the consolesublog.info('Info Message');
Custom transports can also be created
import{Log,LogMessage,Transport}from'@toreda/log';constlog=newLog();// Example dummy example.// Custom actions can perform any async activity.constaction=async(msg:LogMessage):Promise<boolean>=>{returntrue;}// Transports take a string ID, initial log level,// and async action function.consttransport=newTransport('tid1',LogLevels.ALL,action);// Add transport to global listeners.log.addTransport(transport);
Removing Transports
// Remove the same transport// NOTE: Requires access to original transport object// now being removed.log.removeTransport(transport);// Remove global transport by ID.// Use ID to remove global transports if you no// longer have a reference to target transport.log.removeTransportById('tid1');
Install@toreda/log
directly from NPM.
yarn add @toreda/log --dev
npm install @toreda/log --save-dev
Install or clone@toreda/log
(see above).
Toreda Unit Tests useJest.
Installing jest is not required after project dependencies are installed (see above).
yarntest
The next steps are the same whether you installed the package using NPM or cloned the repo from Github.
Enter the following commands in order from the log project root.
yarn build
Enter the following commands in order from the log project root.
npm run-script build
MIT © Toreda, Inc.
Copyright © 2019 - 2022 Toreda, Inc. All Rights Reserved.
About
Lightweight TypeScript logger with flexible custom transports.