Skip to content
Open
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
1,607 changes: 583 additions & 1,024 deletions package-lock.json

Large diffs are not rendered by default.

12 changes: 7 additions & 5 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
".": {
"import": {
"types": "./lib/esm/types/index.d.ts",
"default": "./lib/esm/index.mjs"
"default": "./lib/esm/index.js"
},
"require": {
"types": "./lib/cjs/types/index.d.ts",
Expand All @@ -20,13 +20,14 @@
"lib/**/*"
],
"scripts": {
"clean": "rm -rf ./dist",
"clean": "rm -rf ./lib",
"build": "npm run clean && npm run build:esm && npm run build:cjs",
"build:esm": "tsc -p ./tsconfig.esm.json && mv lib/esm/index.js lib/esm/index.mjs",
"build:cjs": "tsc -p ./tsconfig.cjs.json",
"build:esm": "tsc -p ./tsconfig.esm.json && sh scripts/postbuild-esm",
"build:cjs": "tsc -p ./tsconfig.cjs.json && sh scripts/postbuild-cjs",
"test": "jest --silent",
"semantic-release": "semantic-release",
"prepack": "npm run build"
"prepack": "npm run build",
"prepare": "npm run build"
},
"release": {
"branches": [
Expand Down Expand Up @@ -64,6 +65,7 @@
"homepage": "https://github.com/origranot/ts-logger#readme",
"devDependencies": {
"@types/jest": "^29.4.0",
"@types/node": "^24.5.2",
"jest": "^29.4.2",
"semantic-release": "^20.1.0",
"ts-jest": "^29.0.5"
Expand Down
7 changes: 7 additions & 0 deletions scripts/postbuild-cjs
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
#!/bin/sh

cat >lib/cjs/package.json <<!EOF
{
"type": "commonjs"
}
!EOF
7 changes: 7 additions & 0 deletions scripts/postbuild-esm
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
#!/bin/sh

cat >lib/esm/package.json <<!EOF
{
"type": "module"
}
!EOF
4 changes: 2 additions & 2 deletions src/enums/index.ts
Original file line number Diff line number Diff line change
@@ -1,2 +1,2 @@
export * from './log-level';
export * from './color';
export * from './log-level.js';
export * from './color.js';
4 changes: 2 additions & 2 deletions src/formatters/index.ts
Original file line number Diff line number Diff line change
@@ -1,2 +1,2 @@
export * from './json.formatter';
export * from './simple.formatter';
export * from './json.formatter.js';
export * from './simple.formatter.js';
6 changes: 3 additions & 3 deletions src/formatters/json.formatter.ts
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
import { Formatter, FormatterPayload } from '../interfaces';
import { getTimeStamp, isError, stringify } from '../utils';
import { Formatter, FormatterPayload } from '../interfaces/index.js';
import { getTimeStamp, isError, stringify } from '../utils/index.js';

export class JsonFormatter implements Formatter {
format({ level, args, options }: FormatterPayload): string {
const logData = {
level,
...this.parse(args),
...(options?.timestamp && { timestamp: getTimeStamp(options?.timestamp) }),
...(options?.name && { name: options?.name }),
...(options?.name && { name: options?.name })
};

return stringify(logData);
Expand Down
19 changes: 15 additions & 4 deletions src/formatters/simple.formatter.ts
Original file line number Diff line number Diff line change
@@ -1,14 +1,25 @@
import { Formatter, FormatterPayload } from '../interfaces';
import { colorize, getTimeStamp, isError, DEFAULT_LOG_LEVEL_COLORS, stringify } from '../utils';
import { formatError } from './utils/error-formatter';
import { Formatter, FormatterPayload } from '../interfaces/index.js';
import { colorize, getTimeStamp, isError, DEFAULT_LOG_LEVEL_COLORS, stringify } from '../utils/index.js';
import { formatError } from './utils/error-formatter.js';
import { FormatterOption } from '../interfaces/index.js';

export class SimpleFormatter implements Formatter {
option: FormatterOption = {
color: true
};

constructor(option?: FormatterOption) {
if (option !== undefined) {
this.option = option;
}
}

format({ level, args, options }: FormatterPayload): string {
const { name, timestamp } = options || {};

let prefix: string = '';
prefix += timestamp ? `[${getTimeStamp(timestamp)}] ` : '';
prefix += `${colorize(DEFAULT_LOG_LEVEL_COLORS[level], level)}`;
prefix += this.option.color ? `${colorize(DEFAULT_LOG_LEVEL_COLORS[level], level)}` : `${level}`;
prefix += name ? ` [${name}]` : '';

const message = this.parse(args);
Expand Down
4 changes: 2 additions & 2 deletions src/formatters/utils/error-formatter.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { COLOR } from '../../enums';
import { colorize } from '../../utils';
import { COLOR } from '../../enums/index.js';
import { colorize } from '../../utils/index.js';

export const formatError = (error: Error): string => {
const stackWithoutHeader = error.stack ? error.stack.split('\n').slice(1).join('\n') : '';
Expand Down
10 changes: 5 additions & 5 deletions src/index.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
export * from './enums';
export * from './logger';
export * from './interfaces';
export * from './transports';
export * from './formatters';
export * from './enums/index.js';
export * from './logger.js';
export * from './interfaces/index.js';
export * from './transports/index.js';
export * from './formatters/index.js';
6 changes: 5 additions & 1 deletion src/interfaces/formatter.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { LOG_LEVEL } from '../enums';
import { LOG_LEVEL } from '../enums/index.js';

export interface FormatterPayload {
level: LOG_LEVEL;
Expand All @@ -9,6 +9,10 @@ export interface FormatterPayload {
};
}

export interface FormatterOption {
color: boolean;
}

export interface Formatter {
format(payload: FormatterPayload): string;
}
2 changes: 1 addition & 1 deletion src/interfaces/index.ts
Original file line number Diff line number Diff line change
@@ -1 +1 @@
export * from './formatter';
export * from './formatter.js';
10 changes: 5 additions & 5 deletions src/logger.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { ConsoleTransport } from './transports/console.transport';
import { COLOR, LOG_LEVEL } from './enums';
import { Transport } from './transports';
import { DEFAULT_LOG_LEVEL_COLORS } from './utils';
import { ConsoleTransport } from './transports/console.transport.js';
import { COLOR, LOG_LEVEL } from './enums/index.js';
import { Transport } from './transports/index.js';
import { DEFAULT_LOG_LEVEL_COLORS } from './utils/index.js';

export interface LoggerOptions {
name?: string;
Expand Down Expand Up @@ -41,7 +41,7 @@ export class Logger {
if (this.options.suppress) {
return;
}

for (const transport of this.options.transports!) {
if (level < transport.options.threshold!) {
return;
Expand Down
2 changes: 1 addition & 1 deletion src/transports/console.transport.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { Transport, TransportOptions, TransportPayload } from './transport';
import { Transport, TransportOptions, TransportPayload } from './transport.js';

export interface ConsoleTransportOptions extends TransportOptions {
fullFormat?: boolean;
Expand Down
14 changes: 11 additions & 3 deletions src/transports/file.transport.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import { Transport, TransportOptions, TransportPayload } from './transport';
import { writeFileSync } from 'fs';
import { join } from 'path';
import { Transport, TransportOptions, TransportPayload } from './transport.js';
import { writeFileSync } from 'node:fs';
import { join } from 'node:path';
import { SimpleFormatter } from '../formatters/simple.formatter.js';

type LOG_ROTATION = 'daily' | 'weekly' | 'monthly';

Expand All @@ -11,6 +12,13 @@ export interface FileTransportOptions extends TransportOptions {

export class FileTransport extends Transport {
constructor(private readonly fileOptions: FileTransportOptions) {
// If the user did not specified the formatter, defaults to `SimpleFormatter` without color
// since colored log level won't be properly displayed when the pager or editor does not support
// ANSI colors.
if (fileOptions.formatter === undefined) {
fileOptions.formatter = new SimpleFormatter({ color: false });
}

super(fileOptions);
}

Expand Down
8 changes: 4 additions & 4 deletions src/transports/index.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
export * from './console.transport';
export * from './file.transport';
export * from './udp.transport';
export * from './transport';
export * from './console.transport.js';
export * from './file.transport.js';
export * from './udp.transport.js';
export * from './transport.js';
8 changes: 4 additions & 4 deletions src/transports/transport.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { LOG_LEVEL } from '../enums';
import { SimpleFormatter } from '../formatters';
import { Formatter } from '../interfaces/formatter';
import { LOG_LEVEL } from '../enums/index.js';
import { SimpleFormatter } from '../formatters/index.js';
import { Formatter } from '../interfaces/formatter.js';

export interface TransportOptions {
formatter?: Formatter;
Expand All @@ -17,7 +17,7 @@ export abstract class Transport {
this.options.formatter = this.options.formatter || new SimpleFormatter();
this.options.threshold = this.options.threshold || LOG_LEVEL.DEBUG;
}

public options: TransportOptions;
abstract handle(payload: TransportPayload): void;
}
4 changes: 2 additions & 2 deletions src/transports/udp.transport.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { Transport, TransportOptions, TransportPayload } from './transport';
import { createSocket, Socket } from 'dgram';
import { Transport, TransportOptions, TransportPayload } from './transport.js';
import { createSocket, Socket } from 'node:dgram';

export type SocketType = 'udp4' | 'udp6';

Expand Down
2 changes: 1 addition & 1 deletion src/utils/colors.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { COLOR, LOG_LEVEL } from './../enums';
import { COLOR, LOG_LEVEL } from './../enums/index.js';

export const DEFAULT_LOG_LEVEL_COLORS = {
[LOG_LEVEL.DEBUG]: COLOR.BLUE,
Expand Down
8 changes: 4 additions & 4 deletions src/utils/index.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
export * from './colors';
export * from './timestamps';
export * from './stringify';
export * from './is-error';
export * from './colors.js';
export * from './timestamps.js';
export * from './stringify.js';
export * from './is-error.js';