Skip to content

Commit bece385

Browse files
committed
V 1.0.0
1 parent 30c4395 commit bece385

24 files changed

+3888
-1
lines changed

.eslintignore

+6
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
dist
2+
.github
3+
**/*.js
4+
__tests__
5+
*.spec.ts
6+
*.test.ts

.eslintrc.json

+25
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
{
2+
"env": {
3+
"browser": true,
4+
"es2021": true
5+
},
6+
"extends": [
7+
"eslint:recommended",
8+
"plugin:@typescript-eslint/recommended"
9+
],
10+
"parser": "@typescript-eslint/parser",
11+
"parserOptions": {
12+
"ecmaVersion": "latest",
13+
"sourceType": "module"
14+
},
15+
"plugins": [
16+
"@typescript-eslint"
17+
],
18+
"rules": {
19+
"semi": ["error", "always"],
20+
"@typescript-eslint/semi": ["error", "always"],
21+
"no-console": ["error"],
22+
"no-control-regex": "off",
23+
"no-useless-escape": "off"
24+
}
25+
}

.github/workflows/ci.yml

+31
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
name: CI
2+
on:
3+
pull_request:
4+
paths:
5+
- 'src/**'
6+
- 'package.json'
7+
jobs:
8+
ci:
9+
runs-on: ubuntu-latest
10+
strategy:
11+
matrix:
12+
node-version: ['18.x']
13+
steps:
14+
- uses: actions/checkout@v4
15+
16+
- name: Use Node.js ${{ matrix.node-version }}
17+
uses: actions/setup-node@v3
18+
with:
19+
node-version: ${{ matrix.node-version }}
20+
21+
- name: Install dependencies
22+
run: yarn install
23+
24+
- name: Lint
25+
run: yarn lint
26+
27+
- name: Build
28+
run: yarn build
29+
30+
- name: Run tests
31+
run: yarn test:ci

.github/workflows/publish.yml

+43
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
name: Publish Package to npmjs
2+
on:
3+
push:
4+
branches:
5+
- 'main'
6+
paths:
7+
- 'package.json'
8+
jobs:
9+
tag:
10+
runs-on: ubuntu-latest
11+
env:
12+
CI_COMMIT_EMAIL: [email protected]
13+
CI_COMMIT_AUTHOR: CI Bot
14+
steps:
15+
- uses: actions/checkout@v4
16+
- name: Get tag
17+
id: get_tag
18+
run: |
19+
echo "version=v$(npm pkg get version | tr -d '\"')" >> $GITHUB_OUTPUT
20+
- name: Tag the commit
21+
run: |
22+
git config --global user.name "${{ env.CI_COMMIT_AUTHOR }}"
23+
git config --global user.email "${{ env.CI_COMMIT_EMAIL }}"
24+
next_version=${{ steps.get_tag.outputs.version }}
25+
git tag -a "$next_version" -m "Version $next_version"
26+
git push origin "$next_version"
27+
publish:
28+
runs-on: ubuntu-latest
29+
needs: tag
30+
steps:
31+
- uses: actions/checkout@v4
32+
33+
- uses: actions/setup-node@v3
34+
with:
35+
node-version: '18.x'
36+
- name: Install dependencies
37+
run: yarn install
38+
- run: yarn lint
39+
- run: yarn build
40+
- run: yarn test:ci
41+
- run: yarn pub
42+
env:
43+
NODE_AUTH_TOKEN: ${{ secrets.NPM_TOKEN }}

.npmrc.tpl

+3
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
//registry.npmjs.org/:_authToken=${NODE_AUTH_TOKEN}
2+
@tsxper:registry=https://registry.npmjs.org/
3+
always-auth=true

LICENSE

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
MIT License
22

3-
Copyright (c) 2024 TSXPER
3+
Copyright (c) 2024 Vladyslav Babak
44

55
Permission is hereby granted, free of charge, to any person obtaining a copy
66
of this software and associated documentation files (the "Software"), to deal

Makefile

+28
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
OUT_BUILD := ./dist/
2+
3+
.PHONY: build
4+
build: clean mkdist
5+
npx tsc --declaration --emitDeclarationOnly --declarationDir ./dist/types
6+
npx tsc --project tsconfig.esm.json
7+
npx tsc --project tsconfig.cjs.json
8+
sh ./esm_fix.sh
9+
10+
.PHONY: mkdist
11+
mkdist:
12+
mkdir -p ${OUT_BUILD}
13+
14+
.PHONY: clean
15+
clean:
16+
rm -rf ./dist
17+
18+
.PHONY: prepub
19+
prepub:
20+
cp ./package.json ./dist
21+
cp ./LICENSE ./dist
22+
cp ./README.md ./dist
23+
cp ./.npmrc.tpl ./dist/.npmrc
24+
cp ./package.module.json ./dist/esm/package.json
25+
26+
.PHONY: pub
27+
pub: prepub
28+
cd ./dist && npm publish

README.md

+117
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,117 @@
1+
# Streams Logger
2+
3+
[![NPM Version](https://img.shields.io/npm/v/@tsxper/log-stream.svg?style=flat-square)](https://www.npmjs.com/package/@tsxper/log-stream)
4+
[![License: MIT](https://img.shields.io/badge/License-MIT-yellow.svg?style=flat-square)](LICENSE)
5+
![npm type definitions](https://img.shields.io/npm/types/@tsxper/log-stream)
6+
[![NPM Downloads](https://img.shields.io/npm/dt/@tsxper/log-stream.svg?style=flat-square)](https://www.npmjs.com/package/@tsxper/log-stream)
7+
8+
*@tsxper/log-stream* is a TypeScript stream-based NodeJS logger.
9+
10+
Main features of the *@tsxper/log-stream* are:
11+
- simplicity;
12+
- small size;
13+
- "compact" and colored "visual" output formats;
14+
- logging scopes;
15+
- logging data structures for debugging;
16+
- easy streams replacement;
17+
- circular references safety;
18+
- easy scopes spreading through cloning;
19+
20+
## Configuration
21+
22+
### Output Formats
23+
24+
Currently 2 output formats are supported: "compact" (default) and "visual" (colored or not)
25+
26+
#### Visual Format
27+
![Visual Format](https://raw.githubusercontent.com/tsxper/log-stream/main/examples/visual.png)
28+
29+
#### Compact Format
30+
![Compact Format](https://raw.githubusercontent.com/tsxper/log-stream/main/examples/compact.png)
31+
32+
Compact format contains a JSON string on a new line.
33+
Compact format message decoded:
34+
- t [required], time (ms);
35+
- l [required], log level;
36+
- s [required], scope;
37+
- n [required], name (log message);
38+
- d [optional], data;
39+
- e [optional], error;
40+
41+
```JavaScript
42+
const logger = new Logger(LOG_LEVEL_INFO, 'my service scope');
43+
logger.setOutputFormat('visual');
44+
logger.setColors(false); // disable colors for 'visual' output format
45+
// logger.setColors(true); // applies only to 'visual' output format
46+
// logger.setOutputFormat('compact'); // default
47+
```
48+
49+
### Log Levels
50+
Possible log levels are:
51+
- *LOG_LEVEL_NONE*, or 0, no logs are logged;
52+
- *LOG_LEVEL_ERROR*, or 1, only "error" logs are logged;
53+
- *LOG_LEVEL_WARN*, or 2, "error" and "warn" logs are logged;
54+
- *LOG_LEVEL_INFO*, or 3, "error", "warn" and "info" logs will be logged;
55+
- *LOG_LEVEL_DEBUG*, or 4, "error", "warn", "info" and "debug" logs will be logged;
56+
57+
```JavaScript
58+
// JS example
59+
logger.debug('debug message', [1, 2]);
60+
logger.log('info message');
61+
logger.warn('warn message');
62+
logger.error('error message', new Error('error'));
63+
```
64+
65+
```TypeScript
66+
// TS interface
67+
debug(name: string, data?: unknown): void;
68+
log(name: string, data?: unknown): void;
69+
warn(name: string, data?: unknown): void;
70+
error(name: string, data: Error): void;
71+
```
72+
73+
### Log Scopes
74+
Log scope can be set in a constructor or later in *setScope()*
75+
76+
```TypeScript
77+
constructor(logLevel?: LOG_LEVEL, scope?: string);
78+
setScope(scope: string): this;
79+
```
80+
81+
### Logging Objects
82+
In "compact" output format all *data* objects are converted into JSON strings and are logged "as is".
83+
In "visual" output format or when *data* objects contains circular refs, a "depth" parameter is applied
84+
(by default depth=2).
85+
86+
```TypeScript
87+
setDepth(depth: number): this;
88+
```
89+
90+
**Circular Refs**.
91+
In case passed *data* object contains circular refs, such object is converted into its string representation with denoted circular references.
92+
93+
### Log Streams
94+
Two logs streams are supported: general logs stream and error log stream.
95+
Default log stream for error logs is *process.stderr*.
96+
Default log stream for general logs (all logs that are not errors) is process.stdout.
97+
98+
```TypeScript
99+
// replace log streams
100+
static replaceLogStreams(stdOut: NodeJS.WritableStream, stdErr: NodeJS.WritableStream): void;
101+
```
102+
Calling *Logger.replaceLogStreams()* will make all existing *Logger* instances write into the new streams.
103+
104+
### Clone Logger
105+
Cloning a logger instance makes easy to create a new logger with a new scope but existing setting.
106+
107+
```JavaScript
108+
const logger2 = logger1.clone('new scope');
109+
```
110+
111+
### Formatters
112+
Output formats are configurable through setting custom formatting functions.
113+
114+
```TypeScript
115+
setFormatterCompact(formatter: LOG_FORMATTER): this;
116+
setFormatterVisual(formatter: LOG_FORMATTER): this;
117+
```

esm_fix.sh

+9
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
#!/bin/sh
2+
3+
if [[ "$OSTYPE" == "darwin"* ]]; then
4+
find ./dist/esm -name "*.js" -exec sed -i '' -E "s#export (.*) from '\.(.*)';#export \1 from '.\2\.js';#g" {} +;
5+
find ./dist/esm -name "*.js" -exec sed -i '' -E "s#import (.*) from '\.(.*)';#import \1 from '.\2\.js';#g" {} +;
6+
else
7+
find ./dist/esm -name "*.js" -exec sed -i -E "s#export (.*) from '\.(.*)';#export \1 from '.\2\.js';#g" {} +;
8+
find ./dist/esm -name "*.js" -exec sed -i -E "s#import (.*) from '\.(.*)';#import \1 from '.\2\.js';#g" {} +;
9+
fi

examples/compact.png

58.6 KB
Loading

examples/compact.ts

+3
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
import { log } from './log';
2+
3+
log('compact');

examples/log.ts

+27
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
import { LOG_LEVEL_DEBUG, Logger, OUTPUT_FORMAT } from '../src';
2+
3+
class A {
4+
prop = 'abc';
5+
constructor(public a: A[]) { }
6+
}
7+
8+
export const log = (scope: string, format: OUTPUT_FORMAT = 'compact') => {
9+
const arr: A[] = [];
10+
const obj = new A(arr);
11+
arr.push(obj);
12+
const logger = new Logger(LOG_LEVEL_DEBUG, scope).setOutputFormat(format);
13+
logger.setDepth(1);
14+
logger.log('Circular refs', { arr });
15+
logger.debug('Debug Message', {
16+
k1: 'string 1',
17+
a: {
18+
b: {
19+
c: { e: {} }
20+
},
21+
},
22+
});
23+
logger.log('Info Message');
24+
logger.warn('Unexpected input', { title: '$&^' });
25+
logger.log('hello world', [1, 2]);
26+
logger.error('Error Message', new Error('Invalid data'));
27+
};

examples/visual.png

129 KB
Loading

examples/visual.ts

+3
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
import { log } from './log';
2+
3+
log('expanded', 'visual');

0 commit comments

Comments
 (0)