sfs-logger v1.1.0
SFS-Logger
Before all, this is my first package so I hope it's not too shitty !
This is a lightweigh Logger tool I made to avoid usage of bundled loggers with dependencies.
Getting Started
Prerequisites
To install this package, you need only one thing : NodeJS > 10.15.
You can use it for both Javascript and Typescript.
Installing
To install, just run this command :
npm install --save sfs-logger@latest
How to use
Basic example
Typescript
First you need an instance of Logger class :
import { ILogger, Logger } from "sfs-logger";
const logger: ILogger = new Logger("./logs", "development");
/* First parameter is the path to the directory where you want to store your log files.
Second parameter is the name of the environment application is running on. */
Then you can just use it like following :
import { ILogger, Logger } from "sfs-logger";
const logger: ILogger = new Logger("./logs", "development");
logger.debug("Let's try it !");
logger.error("Wow, incredible !");
logger.info("As sample as it needs to be.");
logger.warn("An other one !");
Javascript
First you need an instance of Logger class :
const sfsLogger = require("sfs-logger");
const logger = new sfsLogger.Logger("./logs", "development");
/* First parameter is the path to the directory where you want to store your log files.
Second parameter is the name of the environment application is running on. */
Then you can just use it like following :
const sfsLogger = require("sfs-logger");
const logger = new sfsLogger.Logger("./logs", "development");
logger.debug("Let's try it !");
logger.error("Wow, incredible !");
logger.info("As sample as it needs to be.");
logger.warn("An other one !");
Advanced usage
If you want to use this logger as your main logger, there are several things to know.
Printing object
You can obviously print object instead of strings if you need to track objects status or for debugging purpose.
For example :
const sfsLogger = require("sfs-logger");
const logger = new sfsLogger.Logger("./logs", "development");
const objectToPrint = { foo: "bar" };
logger.debug(objectToPrint);
Above code will result by an entry similar to this one :
2019-01-01T10:00:00.000Z [DEBUG] {
"foo": "bar"
}
Debug level
Debug level is working only in development and test modes. So if you forget to remove .debug() calls, these messages won't appear in log files.
How to inject
If you use Dependency Injection through your project, you can inject Logger in classes where you need it. I wouldn't recommand it anyway because a logger should not appear in class constructor.
I would rather recommand to instanciate it in a file and to export the created instance like below :
/* myLogger.ts */
import { ILogger, Logger } from "sfs-logger";
const logger: ILogger = new Logger("./logs", "development");
export { logger };
And then to import it wherever it's needed :
/* An other file.ts */
import { logger } from "./**/myFile";
export class FooBar {
foo() {
logger.info("foo() is called");
return "foo";
}
bar() {
logger.info("bar() is called");
return "bar";
}
}
Javascript usage
It's quite similar to Typescript / ES6 usage :
/* myLogger.js */
const sfsLogger = require("sfs-logger");
const logger = new sfsLogger.Logger("./logs", "development");
module.exports = logger;
/* An other file.js */
const logger = require("./path/to/myLogger");
module.exports = class FooBar {
foo() {
logger.info("foo() is called");
return "foo";
}
bar() {
logger.info("bar() is called");
return "bar";
}
}
Running the tests
I used Jasmine to test this package. You can also get coverage thanks to NYC package (these are only used for development purposes).
You can test package by running the following command :
npm test
And you can also get code coverage with the following command :
npm run coverage
Built With
- Typescript - The amazing Javascript subset developed by Microsoft engineers
- NPM - The wonderful dependency manager for Javascript
- TsLint - A great source code linter for Typescript
- Prettierc - An excellent source code formatter
- Jasmine - Famous unit test library
Versioning
We use GitLab for versioning.
Authors
- Sébastien FRANCOIS - Initial work - @francois.sebastien.emile
License
This project is licensed under the MIT License.
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago