UNPKG

3.57 kBMarkdownView Raw
1# TypeScript Abstract Logger
2
3[![Travis](https://img.shields.io/travis/kallaspriit/ts-log.svg)](https://travis-ci.org/kallaspriit/ts-log)
4[![Coverage](https://img.shields.io/coveralls/kallaspriit/ts-log.svg)](https://coveralls.io/github/kallaspriit/ts-log)
5[![Downloads](https://img.shields.io/npm/dm/ts-log.svg)](http://npm-stat.com/charts.html?package=ts-log)
6[![Version](https://img.shields.io/npm/v/ts-log.svg)](http://npm.im/ts-log)
7[![License](https://img.shields.io/npm/l/ts-log.svg)](http://opensource.org/licenses/MIT)
8
9**Abstract logger TypeScript interface along with a dummy logger that does nothing.**
10
11Useful for libraries wanting to provide a pluggable logger that does nothing by default (or provide your own default such as [bunyan](https://github.com/trentm/node-bunyan)).
12
13- Matches the built-in console that can be used directly.
14- Also matches [bunyan](https://github.com/trentm/node-bunyan).
15- Provides usage and custom logger example.
16- Written in TypeScript, no need for extra typings.
17- No dependencies, 24 LOC with comments, 100% test coverage.
18
19## Installation
20
21This package is distributed via npm
22
23```cmd
24npm install ts-log
25```
26```cmd
27yarn add ts-log
28```
29
30## Example
31
32```typescript
33import { dummyLogger, Logger } from "ts-log";
34import * as fs from "fs";
35
36// example class that uses the logger
37class Calculator {
38 // accept the logger in the constructor, defaulting to dummy logger that does nothing
39 public constructor(private readonly log: Logger = dummyLogger) {}
40
41 public sum(a: number, b: number) {
42 const result = a + b;
43
44 // call the logger
45 this.log.info(`summing ${a} + ${b} = ${result}`, a, b, result);
46
47 return result;
48 }
49}
50
51// example custom logger that logs to a file
52class FileLogger implements Logger {
53 private readonly fd: number;
54
55 public constructor(filename: string) {
56 this.fd = fs.openSync(filename, "a");
57 }
58
59 public trace(message?: any, ...optionalParams: any[]): void {
60 this.append("TRACE", `${message} ${JSON.stringify(optionalParams)}`);
61 }
62
63 public debug(message?: any, ...optionalParams: any[]): void {
64 this.append("DEBUG", `${message} ${JSON.stringify(optionalParams)}`);
65 }
66
67 public info(message?: any, ...optionalParams: any[]): void {
68 this.append("INFO ", `${message} ${JSON.stringify(optionalParams)}`);
69 }
70
71 public warn(message?: any, ...optionalParams: any[]): void {
72 this.append("WARN ", `${message} ${JSON.stringify(optionalParams)}`);
73 }
74
75 public error(message?: any, ...optionalParams: any[]): void {
76 this.append("ERROR", `${message} ${JSON.stringify(optionalParams)}`);
77 }
78
79 private append(type: string, message: string) {
80 fs.writeSync(this.fd, `${new Date().toISOString()} ${type} ${message}\n`);
81 }
82}
83
84// don't define a logger, defaults to dummy logger that does nothing
85const calculator1 = new Calculator();
86
87// use the built-in console as the logger
88const calculator2 = new Calculator(console);
89
90// use the custom file logger
91const calculator3 = new Calculator(new FileLogger("log.txt"));
92
93// run the calculator
94calculator1.sum(2, 3);
95calculator2.sum(-4, 1);
96calculator3.sum(6, 3);
97```
98
99## Commands
100
101- `npm start` to start the example application.
102- `npm run build` to build the production version.
103- `npm run test` to run tests.
104- `npm run coverage` to gather code coverage.
105- `npm run lint` to lint the codebase.
106- `npm run prettier` to run prettier.
107- `npm run validate` to run all pre-commit checks (prettier, build, lint, test)
\No newline at end of file