@ripreact/logger
v1.0.3
Published
An opinionated logger for browser devtools.
Downloads
2
Readme
@ripreact/logger
An opinionated logger for browser devtools.
Features:
- Configurable transports.
debug
-like namespaces.- Logging with tags.
- Expressive severity levels.
- Convenient template-based API.
- Flexible and powerful filtering.
- Time differences for segments of namespace and tags.
Devtools transport features:
- Powerline formatting.
- Emoji code.
API
For advanced API docs see src/index.ts
(after Internals
region).
Logger
Creates new logger instance. Must be used without new
keyword.
Severity levels
apocalypse
— App owner should be alerted immediately. Emoji: Fuck!fatal
— App owner should be notified the next morning. Emoji: Rage.crash
— App owner may be notified depending on circumstances. Emoji: Angry.error
— App can recover on its own. Emoji: Shit.warning
— Nothing critical. Emoji: Shrug.ok
— Something good happened. Emoji: Ok.log
— Regular message in development. Emoji: Writing.debug
— Helps you to locate errors. Emoji: Paw.flood
— Use it to flood your console. Emoji: Eyes.
Namespace and tags
Marker = _ (Segment (_ ':'+ _ Segment)*)? (_ '+'+ _ (Tag (_ ','+ _ Tag)*)?)? _
Segment = /[^:+,]/
Tag = /[^:+,]/
_ = /\s*/
Message marker may include namespace and tags. Namespace includes names
separated by :
; tags include names separated by ,
; +
separates namespace
and tags parts. Both segment and tag names may be surrounded by spaces; empty
names will be ignored. You cannot use :
, +
, ,
in names.
Substitutions
@ripreact/logger
doesn’t provide string formatting support by default; you may
use transport, which supports it (e.g. devtools transport).
Usage
const logger = Logger({
/**
* Array of plugins.
*/
plugins: [LevelFilter({ level: 'flood' }), DevtoolsTransport()],
});
// Levels:
logger.apocalypse`foo:bar`('App owner should be alerted immediately.');
logger.disaster`foo:bar`('App owner should be notified the next morning.');
logger.crash`foo:bar`('App owner may be notified depending on circumstances.');
logger.error`foo:bar`('App can recover on its own.');
logger.warning`foo:bar`('Nothing critical.');
logger.ok`foo:bar`('Something good happened.');
logger.log`foo:bar`('Regular message in development.');
logger.trace`foo:bar`('Helps you to locate errors.');
logger.flood`foo:bar`('Use it to flood your console.');
// Namespace and tags:
logger.log`company:product:package:class:method +tag1, tag2`('Foo bar.');
DevtoolsTransport
const logger = Logger({
plugins: [
DevtoolsTransport({
/**
* Transport-specific message preprocessor.
*/
preprocessor: message => message,
/**
* Transport-specific filter. Applied before preprocessor.
*/
filterMessages: () => true,
/**
* Segments filter; allows to hide specific segments (e.g company
* name prefix).
*/
filterSegments: ({ name }, index) => name != 'cyka' || index > 0,
/**
* Tags filter; allows to hide specific tags.
*/
filterTags: ({ name }) => !/^user=\d+/.test(name),
}),
],
});
LevelFilter
Filters messages by severity level.
const logger = Logger({
plugins: [
LevelFilter({
/**
* Most verbose allowed level. Defaults to `'warning'`.
*/
level: 'debug',
}),
],
});
PrefixPreprocessor
Prepends specific prefix (e.g. company/product/package name) to each namespace.
const logger = Logger({
plugins: [
PrefixPreprocessor({
prefix: ['cyka', 'blyat'],
}),
],
});
getMarker
Returns message marker string (normalized namespace and normalized deduped sorted tags).
const logger = Logger({
plugins: [
{
preprocessor(message) {
console.log(getMarker(message)); // ¯\_(ツ)_/¯
},
},
],
});
logger.log` : : a : : b : : + + , , c , , d , , +d, e`(); // `a:b +c,d`
logger
Default instance of logger. Provides default level filter and default transport.
Roadmap
- [ ] NodeJS console transport.
- [ ] HTTP transport.
- [ ] File transport.
- [ ] WebSocket transport.
- [ ] journald transport.
- [ ] Event Log transport.
- [ ] Stack traces.
- [ ] High resolution time.
- [ ]
debug
compatibility layer. - [ ] Gotta Go Fast.