Swaler makes you easy and simple to log message in your web app, but not only that!
It has been developed on top of javascript console and support the following methods: .trace
, .debug
, .info
, .warn
, .error
npm install swaler
import {Logger} from 'swaler';
// A simple logger
const logger = new Logger();
logger.debug('Hello there!'); // Hello there!
// A logger with a Context
const loggerWithCtx = new Logger('Dalaran');
loggerWithCtx.debug('A new expansion has been detected...');
loggerWithCtx.info(`Time to go to Northrend.`);
loggerWithCtx.warn('Attention', 'to', 'take off!');
loggerWithCtx.error('A %s has been found!', 'crater');
// [Dalaran] A new expansion has been detected...
// [Dalaran] Time to go to Northrend.
// [Dalaran] Attention to take off!
// [Dalaran] A crater has been found!
// Several ways to create a logger
const logger = new Logger();
const logger = new Logger(context);
const logger = new Logger(null, opts);
const logger = new Logger(context, opts);
type: string
default: null
Give the logger a specific context that will be prompt between brackets
Very useful when, for examples, you want to log messages in some of your components without having to specify the component name each time you log something!
// Example using Context
import {Swaler} from 'swaler';
class AuthService {
constructor() {
this.logger = new Swaler('AuthService');
}
signIn() {
// ... your awesome sign in implementation ...
this.logger.info('User has successfully signed in!');
// ... or maybe ...
this.logger.error('An error occurred when trying to sign in user');
}
}
// [AuthService] User has successfully signed in!
// [AuthService] An error occurred when trying to sign in user!
type: SwalerLevel
default: null
Set the minimal level of log to display
import {Swaler, SwalerLevels} from 'swaler';
const logger = new Swaler(null, {level: SwalerLevels.WARN});
logger.debug("Won't be logged!");
logger.info("Won't be logged!");
logger.warn('Will be logged!');
logger.error('Will be logged!');
All log methods are built on top of console which means that each Swaler log methods act the same way than console methods!
Display a trace log
logger.trace('A trace message');
Display a debug message
logger.debug('A debug message');
Display an info message
logger.info('A info message');
Display a warn message
logger.warn('A warn message');
Display a error message
logger.error('An error message');
Sometimes, you may need to pass options when calling a log. To do so, use withLogOptions()
methods
logger
.withLogOptions({
ignoreLevel: boolean,
})
.info('An info message with options');
options | Type | Description |
---|---|---|
ignoreLevel | boolean | Make the log ignoring the level of the logger |
const logger = new Logger('AuthModule', {level: SwalerLevels.WARN});
logger
.withLogOptions({ignoreLevel: true})
.info('Will be logged despite the WARN levels!');
type: SwalerLevels
default: SwalerLevels.DEBUG
By default, all loggers will use the defaultLevel
if you do not pass a opts.level
.
import {Swaler, SwalerLevels} from 'swaler';
Swaler.defaultLevel =
process.env.NODE_ENV === 'production'
? SwalerLevels.WARN
: SwalerLevels.DEBUG;
const logger = new Swaler();
const loggerWithLevel = new Swaler(null, {level: SwalerLevels.INFO});
logger.info("Won't be logged");
loggerWithLevel.info('Will be logged');
Check this article on how to manage front-end js env variable
You are welcome to contribute, see Contributing.