Skip to content

Commit

Permalink
Merge pull request #18 from deno-library/ISSUE_15_loglevel
Browse files Browse the repository at this point in the history
[#15] add debug and log as loglevel
  • Loading branch information
UrielCh authored Dec 22, 2024
2 parents e88837a + 863855b commit a56c71b
Show file tree
Hide file tree
Showing 7 changed files with 134 additions and 10 deletions.
6 changes: 6 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,9 @@ import { Logger } from "jsr:@deno-library/logger";

const logger = new Logger();

logger.debug("i am from consoleLogger", "debug");
logger.info("i am from consoleLogger", { name: "zfx" });
logger.log("i am from consoleLogger", "hello");
logger.warn("i am from consoleLogger", 1, "any");
logger.error("i am from consoleLogger", new Error("test"));
```
Expand All @@ -33,14 +35,18 @@ import { Logger } from "jsr:@deno-library/logger";
const logger = new Logger();

// console only
logger.debug("i am from consoleLogger", "debug");
logger.info("i am from consoleLogger", { name: "zfx" });
logger.log("i am from consoleLogger", "hello");
logger.warn("i am from consoleLogger", 1, "any");
logger.error("i am from consoleLogger", new Error("test"));

await logger.initFileLogger("../log");

// file and console
logger.debug("i am from consoleLogger", "debug");
logger.info("i am from fileLogger", { name: "zfx" });
logger.log("i am from consoleLogger", "hello");
logger.warn("i am from fileLogger", 1, "any");
logger.error("i am from fileLogger", new Error("test"));
```
Expand Down
15 changes: 15 additions & 0 deletions deno.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

38 changes: 34 additions & 4 deletions deps.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,34 @@
export { green, red, stripAnsiCode, yellow } from "jsr:@std/fmt@1.0.3/colors";
// UNSTABLE
export { writeAll } from "jsr:@std/io@0.225.0/write-all";
export { exists } from "jsr:@std/fs@1.0.5";
export {
cyan,
gray,
green,
red,
stripAnsiCode,
yellow,
} from "jsr:@std/fmt@1.0.3/colors";

/**
* use a local copy of UNSTABLE { type Writer, writeAll } from "jsr:@std/io@0.225.0/write-all";
*/
export interface Writer {
/** Writes `p.byteLength` bytes from `p` to the underlying data stream. It
* resolves to the number of bytes written from `p` (`0` <= `n` <=
* `p.byteLength`) or reject with the error encountered that caused the
* write to stop early. `write()` must reject with a non-null error if
* would resolve to `n` < `p.byteLength`. `write()` must not modify the
* slice data, even temporarily.
*
* Implementations should not retain a reference to `p`.
*/
write(p: Uint8Array): Promise<number>;
}

export async function writeAll(writer: Writer, data: Uint8Array) {
let nwritten = 0;
while (nwritten < data.length) {
nwritten += await writer.write(data.subarray(nwritten));
}
}
export { exists } from "jsr:@std/fs@^1.0.6";

export { stat } from "node:fs/promises";
73 changes: 68 additions & 5 deletions logger.ts
Original file line number Diff line number Diff line change
@@ -1,15 +1,17 @@
import stdout from "./stdout.ts";
import Writer from "./writer.ts";
import eol from "./eol.ts";
import { exists } from "./deps.ts";
import Dater from "./date.ts";
import { green, red, stripAnsiCode, yellow } from "./deps.ts";
import { cyan, exists, green, red, stripAnsiCode, yellow } from "./deps.ts";
import type { fileLoggerOptions, LoggerWriteOptions } from "./interface.ts";
import Types from "./types.ts";

const { inspect } = Deno;

const noop = async () => {};

export type LoggerType = "debug" | "info" | "log" | "warn" | "error";

/**
* Logger class
*/
Expand All @@ -20,7 +22,9 @@ export default class Logger {
private rotate = false;
private dir?: string;

#debug = this.debug;
#info = this.info;
#log = this.log;
#warn = this.warn;
#error = this.error;
#write = this.write;
Expand All @@ -38,6 +42,21 @@ export default class Logger {
return this.encoder.encode(stripAnsiCode(msg) + eol);
}

/**
* Log message with debug level
* @param args data to log
*/
async debug(...args: unknown[]): Promise<void> {
args = [this.getDebug(), ...args];
this.stdout(...args);
if (this.dir) {
await this.write({
dir: this.dir,
type: Types.DEBUG,
args,
});
}
}
/**
* Log message with info level
* @param args data to log
Expand All @@ -54,6 +73,22 @@ export default class Logger {
}
}

/**
* Log message with info level
* @param args data to log
*/
async log(...args: unknown[]): Promise<void> {
args = [this.getLog(), ...args];
this.stdout(...args);
if (this.dir) {
await this.write({
dir: this.dir,
type: Types.LOG,
args,
});
}
}

/**
* Log message with warning level
* @param args data to log
Expand Down Expand Up @@ -126,17 +161,27 @@ export default class Logger {
* disable a specific type of logger
* @param type Level of logger to disable
*/
disable(type?: "info" | "warn" | "error"): void {
disable(type?: LoggerType): void {
if (!type) {
this.debug = noop;
this.info = noop;
this.log = noop;
this.warn = noop;
this.error = noop;
return;
}
if (type === "debug") {
this.debug = noop;
return;
}
if (type === "info") {
this.info = noop;
return;
}
if (type === "log") {
this.log = noop;
return;
}
if (type === "warn") {
this.warn = noop;
return;
Expand All @@ -151,16 +196,26 @@ export default class Logger {
* Enable a specific type of logger
* @param type Level of logger to enable
*/
enable(type?: "info" | "warn" | "error"): void {
enable(type?: LoggerType): void {
if (!type) {
this.debug = this.#debug;
this.info = this.#info;
this.log = this.#log;
this.warn = this.#warn;
this.error = this.#error;
}
if (type === "debug") {
this.debug = this.#debug;
return;
}
if (type === "info") {
this.info = this.#info;
return;
}
if (type === "log") {
this.log = this.#log;
return;
}
if (type === "warn") {
this.warn = this.#warn;
return;
Expand Down Expand Up @@ -199,8 +254,16 @@ export default class Logger {
this.write = this.#write;
}

private getDebug(): string {
return green(this.getNow() + cyan(` Debug:`));
}

private getInfo(): string {
return green(`${this.getNow()} Info:`);
return green(this.getNow() + green(` Info:`));
}

private getLog(): string {
return green(`${this.getNow()} Log:`);
}

private getWarn(): string {
Expand Down
8 changes: 7 additions & 1 deletion mod.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,9 @@ import { Logger } from "jsr:@deno-library/logger";
const logger = new Logger();
logger.debug("i am from consoleLogger", "debug");
logger.info("i am from consoleLogger", { name: "zfx" });
logger.log("i am from consoleLogger", "hello");
logger.warn("i am from consoleLogger", 1, "any");
logger.error("i am from consoleLogger", new Error("test"));
```
Expand All @@ -30,14 +32,18 @@ import { Logger } from "jsr:@deno-library/logger";
const logger = new Logger();
// console only
logger.debug("i am from consoleLogger", "debug");
logger.info("i am from consoleLogger", { name: "zfx" });
logger.log("i am from consoleLogger", "hello");
logger.warn("i am from consoleLogger", 1, "any");
logger.error("i am from consoleLogger", new Error("test"));
await logger.initFileLogger("../log");
// file and console
logger.debug("i am from consoleLogger", "debug");
logger.info("i am from fileLogger", { name: "zfx" });
logger.log("i am from consoleLogger", "hello");
logger.warn("i am from fileLogger", 1, "any");
logger.error("i am from fileLogger", new Error("test"));
```
Expand Down Expand Up @@ -307,7 +313,7 @@ interface LoggerInerface {
/**
* The Logger class
*/
export { default as Logger } from "./logger.ts";
export { default as Logger, type LoggerType } from "./logger.ts";
/**
* The Logger class default instance
*/
Expand Down
2 changes: 2 additions & 0 deletions types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,9 @@
* Types of messages that can be logged.
*/
export const enum Types {
DEBUG = "debug",
INFO = "info",
LOG = "log",
WARN = "warn",
ERROR = "error",
}
Expand Down
2 changes: 2 additions & 0 deletions writer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,9 @@ export default class Writer {
private maxBackupCount?: number;
private pathWriterMap = new Map();

private [Types.DEBUG]: string = "";
private [Types.INFO]: string = "";
private [Types.LOG]: string = "";
private [Types.WARN]: string = "";
private [Types.ERROR]: string = "";

Expand Down

0 comments on commit a56c71b

Please sign in to comment.