Skip to content

Commit

Permalink
chore: Initial commit.
Browse files Browse the repository at this point in the history
  • Loading branch information
miyaokamarina committed Oct 29, 2019
0 parents commit b97e689
Show file tree
Hide file tree
Showing 20 changed files with 3,271 additions and 0 deletions.
4 changes: 4 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
idea/
dist/
node_modules/
*.iml
38 changes: 38 additions & 0 deletions .idea/$CACHE_FILE$

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 2 additions & 0 deletions .idea/.gitignore

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

5 changes: 5 additions & 0 deletions .idea/codeStyles/codeStyleConfig.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

6 changes: 6 additions & 0 deletions .idea/dictionaries

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

9 changes: 9 additions & 0 deletions .idea/inspectionProfiles/Project_Default.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

6 changes: 6 additions & 0 deletions .idea/jsLibraryMappings.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

9 changes: 9 additions & 0 deletions .idea/misc.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

8 changes: 8 additions & 0 deletions .idea/modules.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

6 changes: 6 additions & 0 deletions .idea/vcs.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 3 additions & 0 deletions .npmignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
.idea/
node_modules/
*.iml
181 changes: 181 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,181 @@
# @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

1. `apocalypse` — App owner should be alerted immediately. Emoji: Fuck!
2. `fatal` — App owner should be notified the next morning. Emoji: Rage.
3. `crash` — App owner may be notified depending on circumstances. Emoji: Angry.
4. `error` — App can recover on its own. Emoji: Shit.
5. `warning` — Nothing critical. Emoji: Shrug.
6. `ok` — Something good happened. Emoji: Ok.
7. `log` — Regular message in development. Emoji: Writing.
8. `debug` — Helps you to locate errors. Emoji: Paw.
9. `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

```javascript
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`

```javascript
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.

```javascript
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.

```javascript
const logger = Logger({
plugins: [
PrefixPreprocessor({
prefix: ['cyka', 'blyat'],
}),
],
});
```

### `getMarker`

Returns message marker string (normalized namespace and normalized deduped
sorted tags).

```javascript
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.
18 changes: 18 additions & 0 deletions babel.config.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
module.exports = {
presets: [
[
'@babel/preset-env',
{
modules: 'commonjs',
shippedProposals: true,
useBuiltIns: 'usage',
corejs: 3,
targets: {
chrome: '77',
},
include: ['transform-block-scoping'],
},
],
'@babel/preset-typescript',
],
};
22 changes: 22 additions & 0 deletions example.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
const { Logger, NodeTransport } = require('.');

const logger = Logger({
filter: () => true,
prefix: 'demo:',
transports: [NodeTransport({ timestamps: true })],
});

const wait = n => new Promise(resolve => setTimeout(resolve, n));

(async () => {
logger.log`foo:bar`('%o', 'This is error message.');
await wait(10);

logger.log`foo:bar`('%o', '`+Nms` is time since last `demo:foo:bar` message.');
await wait(10);

logger.log`foo:xyi`('%o', 'Time diff in this message shows time since last message with `demo:foo` prefix.');
await wait(10);

logger.log`foo:bar`('%o', 'Diffs in this message shown for both `demo:foo` and `demo:foo:bar`');
})();
Loading

0 comments on commit b97e689

Please sign in to comment.