Skip to content

Commit

Permalink
Docs
Browse files Browse the repository at this point in the history
  • Loading branch information
Hexagon committed Mar 18, 2024
1 parent 4a40b89 commit 145788f
Show file tree
Hide file tree
Showing 6 changed files with 55 additions and 25 deletions.
37 changes: 37 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
21 changes: 12 additions & 9 deletions mod.ts
Original file line number Diff line number Diff line change
@@ -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";
2 changes: 1 addition & 1 deletion src/log.ts
Original file line number Diff line number Diff line change
@@ -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";

/**
Expand Down
2 changes: 1 addition & 1 deletion src/transport.ts → transports/base.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { NumericSeverity, Severity } from "./types.ts";
import { NumericSeverity, Severity } from "../src/types.ts";

export interface LogTransportBaseOptions {
/**
Expand Down
15 changes: 3 additions & 12 deletions transports/console.ts
Original file line number Diff line number Diff line change
@@ -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 {
Expand Down Expand Up @@ -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:
Expand Down Expand Up @@ -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(" ")}`;
}
}
3 changes: 1 addition & 2 deletions transports/file.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down

0 comments on commit 145788f

Please sign in to comment.