Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Minor logger refactor, add documentation #42

Merged
merged 1 commit into from
Sep 27, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 4 additions & 1 deletion packages/common/README.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
# common

Utilities commonly used by other packages.
Utilities commonly used by other packages. Each common utility lives in its own folder together with its documentation.
The implementation is re-exported by main entry of this package. The package consists of:

- [logger](./src/logger) - Backend-only logger for Node.js packages based on Winston logger.

## Getting started

Expand Down
13 changes: 7 additions & 6 deletions packages/common/src/index.test.ts
Original file line number Diff line number Diff line change
@@ -1,12 +1,13 @@
import { readFileSync, readdirSync } from 'fs';
import { join } from 'path';

it('index file re-exports from all implementation files', () => {
const files = readdirSync(__dirname);
const ignoreList = ['index.ts'];
it('index file re-exports from all implementation folders', () => {
const entries = readdirSync(__dirname, { withFileTypes: true });

const implementationFiles = files.filter((file) => !file.endsWith('.test.ts') && !ignoreList.includes(file));
const exports = [...readFileSync(join(__dirname, './index.ts'), 'utf8').matchAll(/export \* from/g)];
const subFolders = entries.filter((entry) => entry.isDirectory()).map((dir) => dir.name);
const mainExports = [
...readFileSync(join(__dirname, './index.ts'), 'utf8').matchAll(/export \* from '\.\/(.+)'/g),
].map((match) => match[1]);

expect(implementationFiles.length).toEqual(exports.length);
expect(subFolders).toEqual(mainExports);
});
38 changes: 38 additions & 0 deletions packages/common/src/logger/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
# Logger

Backend-only logger for Node.js packages based on Winston logger.

## Configuration

Logger configuration is essentially:

```ts
interface LogConfiguration {
type: 'hidden' | 'json' | 'pretty'; // Specifies the log format.
styling: 'on' | 'off'; // Toggles output colorization.
minLevel: 'debug' | 'info' | 'warn' | 'error'; // Specifies the minimum log level that is logged.
}
```

### type

- `hidden` - Silences all logs. This is suitable for test environment.
- `json` - Specifies JSON log format. This is suitable when running in production and streaming logs to other services.
- `pretty` - Logs are formatted in a human-friendly "pretty" way. Ideal, when running the service locally and in
development.

### styling

- `on` - Enables colors in the log output. The output has special color setting characters that are parseable by CLI.
Recommended when running locally and in development.
- `off` - Disables colors in the log output. Recommended for production.

### minLevel

One of the following options:

```ts
'debug' | 'info' | 'warn' | 'error';
```

Logs with smaller level (severity) will be silenced.
File renamed without changes.