diff --git a/README.md b/README.md index d7fb73f..7a42179 100644 --- a/README.md +++ b/README.md @@ -102,6 +102,43 @@ try { } ``` +#### Creating a custom transport + +```ts +import { Log, LogTransportBase, LogTransportBaseOptions } from "./mod.ts"; +import { Severity } from "./mod.ts"; + +/** + * Create your own Transport by extending LogTransportBase + */ +export class CustomLogger extends LogTransportBase { + options: LogTransportBaseOptions; + constructor(options?: LogTransportBaseOptions) { + super(); + this.options = { ...this.defaults, ...options }; + } + log(level: Severity, scope: string, data: unknown[], timestamp: Date) { + if (this.shouldLog(level)) { + // Custom implementation below + const formattedMessage = `${timestamp.toISOString()} ${level} ${scope} ${ + data.join(" ") + }`; + if (level === Severity.Error) { + console.error(formattedMessage); + } else { + console.log(formattedMessage); + } + } + } +} + +// Create a new logger, using our custom logger +const logger = new Log([new CustomLogger()]); + +// Log a message +logger.warn("This is a warning"); +``` + **Contributions and Feedback** We welcome your contributions to improve `@cross/log`! Submit issues, feature diff --git a/mod.ts b/mod.ts index b28053b..56fc09c 100644 --- a/mod.ts +++ b/mod.ts @@ -1,15 +1,18 @@ /** - * Re-export of common transports - * - * All transports is available by using - * - * ``` - * import { FileLogger } from "@cross/log/file"; - * import { ConsoleLogger } from "@cross/log/console"; - * // ... etc - * ``` + * Re-export of common features */ export { Log } from "./src/log.ts"; export { Severity } from "./src/types.ts"; + +/** + * Re-export of common transports + */ export { ConsoleLogger } from "./transports/console.ts"; export { FileLogger } from "./transports/file.ts"; + +/** + * Re-export of transport classes + */ +// deno-lint-ignore-file +export { LogTransportBase } from "./transports/base.ts"; +export type { LogTransportBaseOptions } from "./transports/base.ts"; diff --git a/src/log.ts b/src/log.ts index 464d11c..7dfdfc6 100644 --- a/src/log.ts +++ b/src/log.ts @@ -1,5 +1,5 @@ import { ConsoleLogger } from "../transports/console.ts"; -import { LogTransport } from "./transport.ts"; +import { LogTransport } from "../transports/base.ts"; import { Severity } from "./types.ts"; /** diff --git a/src/transport.ts b/transports/base.ts similarity index 97% rename from src/transport.ts rename to transports/base.ts index cfde708..60992cf 100644 --- a/src/transport.ts +++ b/transports/base.ts @@ -1,4 +1,4 @@ -import { NumericSeverity, Severity } from "./types.ts"; +import { NumericSeverity, Severity } from "../src/types.ts"; export interface LogTransportBaseOptions { /** diff --git a/transports/console.ts b/transports/console.ts index ed97cb9..20f5623 100644 --- a/transports/console.ts +++ b/transports/console.ts @@ -1,7 +1,7 @@ // deno-lint-ignore-file import { Colors } from "@cross/utils"; import { deepMerge } from "@cross/deepmerge"; -import { LogTransportBase, LogTransportBaseOptions } from "../src/transport.ts"; +import { LogTransportBase, LogTransportBaseOptions } from "./base.ts"; import { Severity } from "../src/types.ts"; interface ConsoleLoggerOptions extends LogTransportBaseOptions { @@ -40,7 +40,8 @@ export class ConsoleLogger extends LogTransportBase { const timestampText = Colors.dim(timestamp.toISOString()); let styledLevel = level.toString().padEnd(5, " "); - let message = this.formatMessage(data, scope); // Construct message + + let message = `${scope}: ${data.join(" ")}`; switch (level) { case Severity.Debug: @@ -68,14 +69,4 @@ export class ConsoleLogger extends LogTransportBase { } } } - - /** - * Helper to format the log message. - * @param data - Array of data to be logged. - * @param scope - Optional scope or category of the message. - * @returns The formatted log message string. - */ - private formatMessage(data: unknown[], scope: string): string { - return `${scope}: ${data.join(" ")}`; - } } diff --git a/transports/file.ts b/transports/file.ts index 79a634c..f959182 100644 --- a/transports/file.ts +++ b/transports/file.ts @@ -3,9 +3,8 @@ import { LogTransport, LogTransportBase, LogTransportBaseOptions, -} from "../src/transport.ts"; +} from "./base.ts"; import { Severity } from "../src/types.ts"; - import { deepMerge } from "@cross/deepmerge"; interface FileLoggerOptions extends LogTransportBaseOptions {