Skip to content
This repository has been archived by the owner on Apr 13, 2024. It is now read-only.

Commit

Permalink
2.1.0 - custom levels
Browse files Browse the repository at this point in the history
  • Loading branch information
TorchedSammy committed Sep 30, 2020
1 parent 53266a2 commit 1d39395
Show file tree
Hide file tree
Showing 4 changed files with 110 additions and 19 deletions.
98 changes: 88 additions & 10 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,34 +1,112 @@
# Loggaby
> 📝 A minimal and simplistic logger with no bloat.
<div align="center">
<h1>Loggaby</h1>
<blockquote align="center">📝 A minimal and simplistic logger with no bloat.</blockquote>
<p>
<a href="https://github.com/Luvella/Loggaby/blob/master/LICENSE">
<img alt="GitHub license" src="https://img.shields.io/github/license/Luvella/Loggaby?style=for-the-badge">
</a>
<a href="https://github.com/Luvella/Loggaby/stargazers">
<img alt="GitHub stars" src="https://img.shields.io/github/stars/Luvella/Loggaby?style=for-the-badge">
</a>
<br>
Loggaby aims to be a simple, minimalistic and lightweight logger that doesn't bring useless dependencies and extra unneeded features.
It has 1 goal and strives for it well: Posting nice looking logs to your terminal (or perhaps a custom transport?!? See <a href="#example">the examples</a> for details)
<br><br>
Oh, it looks like this:<br>
<img alt="Preview" src="https://modeus.is-inside.me/V6nRi6i6.png">
</p>
</div>

Loggaby aims to be a simple, minimalistic and lightweight logger that doesn't bring useless dependencies and extra unneeded features.
It has 1 goal and strives for it well: Posting nice looking logs to your terminal (or perhaps a custom transport?!? See [the example](#example) for details).
# Table of Contents
- [Install](#install)
- [Examples](#examples)
- [Docs](#documentatiob)
- [License](#license)

Oh, it looks like this:
![](https://modeus.is-inside.me/V6nRi6i6.png)
## Install
`npm i loggaby`

## Example
## Examples
```js
const Loggaby = require('loggaby');
const TerminalTransport = new Loggaby.TerminalTransport();
const logger = new Loggaby({
transports: [TerminalTransport] // NOTE: This is provided by default. There is no reason
// to import TerminalTransport unless adding your own extra transports.
// For an implementation example of a Transport, see `lib/transports/TerminalTransport.js`
// For an implementation example of a Transport, see below.
});

logger.log('Hello World!');
```

Transport Example:
```js
// CustomTransport.js
// It's essentially just the TerminalTransport without color.
// Realistically you can just pass `false` to the TerminalTransport constructor
const Loggaby = require('loggaby');
class CustomTransport extends Loggaby.Transport {
constructor() {
super(false) // Disable color
}

transmit(msg) { // The function to post/print a message
console.log(msg)
}
}

module.exports = CustomTransport;

// index.js
const Loggaby = require('loggaby');
const CustomTransport = require('./CustomTransport');
const logger = new Loggaby({
transports: [new CustomTransport()]
});

logger.log('Hello World!');
```

Custom levels:
```js
const Loggaby = require('loggaby');

const logger = new Loggaby({
levels: [
{
name: 'API',
color: 'magenta'
},
{
// Overriding default levels is also an option
name: 'Log',
color: 'yellow'
}
]
});

logger.log('Hello World!');
logger.api('Online at port 3000!');
```
![](https://modeus.is-inside.me/HzSP9TCd.png)

# Documentation
#### new Loggaby(options)
The Loggaby constructor, which creates a `logger` instance.
- `options` {Object}
- `transports` {Object[]} (An array of objects or specifically [transport instances](lib/transports/)) What transports to log to. An example is provided above. (Default: `[TerminalTransport]`)
- `transports` {Object[]} (An array of objects or specifically [transport instances](lib/transports/)) What transports to log to. An example is provided above. (Default: `[TerminalTransport]`)
- `debug` {Boolean} Whether to print debug messages. (Default: `true`)
- `levels` {Object[]} Additional custom levels to provide.
- `name` {String} Name of the level
- `color` {String} Color of the level (accepted values are [here](https://github.com/Luvella/AnsiKit#colors))
- `debug` {Boolean} Whether this is a debug log (hidden with `debug: false`)
- `fatal` {Boolean} Whether to make the level name and message bold and underline

## Levels
`debug`, `log`, `warn`, `error`, `fatal`

You can log with `logger.<Level>()` [for example](#example).
You can log with `logger.<Level>()` [for example](#example).

# License
Loggaby is licensed under the MIT license.
[Read here](LICENSE) for more info.
7 changes: 5 additions & 2 deletions lib/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,10 @@ class Loggaby {
if (Array.isArray(options) || typeof options !== 'object' && options !== null) throw new TypeError('"options" has to be an object.');
this.options = Object.assign({
debug: true,
transports: [new TerminalTransport()]
transports: [new TerminalTransport()],
levels: []
}, options);
if (!Array.isArray(this.options.levels)) throw new TypeError('levels should be an array of objects');
this.transports = this.options.transports;

const loggers = [
Expand All @@ -33,7 +35,8 @@ class Loggaby {
color: 'red',
name: 'Fatal',
fatal: true
}
},
...this.options.levels
];

for (const logger of loggers) {
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "loggaby",
"version": "2.0.0",
"version": "2.1.0",
"description": "📝 A minimal and simplistic logger with no bloat.",
"main": "lib/index.js",
"scripts": {
Expand Down
22 changes: 16 additions & 6 deletions test.js
Original file line number Diff line number Diff line change
@@ -1,8 +1,18 @@
const Loggaby = require("./lib/index");
const logger = new Loggaby();

logger.debug("Test");
logger.log("Test");
logger.warn("Test");
logger.error("Test");
logger.fatal("Test");
const logger = new Loggaby({
levels: [
{
name: 'API',
color: 'magenta'
},
{
// Overriding default levels is also an option
name: 'Log',
color: 'yellow'
}
]
});

logger.log('Hello World!');
logger.api('Online at port 3000!')

0 comments on commit 1d39395

Please # to comment.