Loggy, better than a simple console
Let's be honest. There are more wraps of the console than pieces of hay are in a haystack. Loggy offers a simpler way to use the console. It allows logging inside groups, which comes in handy to filter through your browser console when you mess up Angular detection change. Loggy also lets you log methods and properties by simply adding a decorator. And all this, while maintaining the file name and line.
- ✅ Log by group
- ✅ Stylish log levels
- ✅ Log methods
- ✅ Log properties
npm install @lapita/loggy --save
yarn add @lapita/loggy
Start using Loggy couldn't be simplier. You need to create a file, preferably inside your src folder. Inside this file, you need to set the log level first, and then create an instance of Loggy by calling createLoggy
.
This creation method requires a single object with contains the list of groups you want for your project. Some examples are login, authorization, and so on. These groups will make easier to filter your browser Console when you need to look for specific information associated to a component.
import { createLoggy, setLogLevel } from '@lapita/loggy';
import { environment } from './environments/environment';
// Set the log level
setLogLevel(environment.logLevel);
// Instance loggy
export const {debug, log, info, warn, error} = createLoggy(
{
global: 'global stuff',
auth: 'authorization',
dashboard: 'dashboard'
}
);
Loggy wraps five log levels. From lower to higher level, you'll find debug, log, info, warn and error. There can be easily access by destructuring the return object. And you're ready to start using Loggy.
log.global('It preserves the line numbers', {obj: 'test'});
log.auth('auth failed');
log.dashboard('dashboard init');
Your IDE will suggest the available groups as soon as you type log.
.
Logging a property can be done by adding a @LoggyProperty
. You need to pass in a function pointing to the log and group you want to use. This way, you'll be preserving the line and file where the property lays.
@LoggyProperty((t) => log.global(t), {logValue: true, accessType: 'set'})
title = 'Loggy! Open the console.';
This decorator admits the following options, which are self-explanatory.
{
logCalls: boolean;
logValue: boolean;
accessType: 'get' | 'set' | 'get/set';
enable: boolean;
}
Logging a method can be done by adding a @LoggyMethod
. You need to pass in a function pointing to the log and group you want to use as well.
@LoggyMethod((t) => log.global(t))
testMethod() {
for (let index = 0; index < 1000000; index++) {
const text = String(index);
const number = +text;
}
}
This decorator admits the following options, which, again, are pretty self-explanatory.
{
logTime: true,
logCalls: false,
logContext: false,
enable: true,
}
Addionally, it is possible to customize the log level colors as well as the separator between the group name and the actually log text. You need to pass in another argument while creating a new instace of Loggy.
{
separator: '#',
debug: 'background: white; color: black;',
log: 'background: purple; color: white;',
info: 'background: blue; color: white;',
warn: 'background: orange; color: black;',
error: 'background: red; color: white;',
};