@denodnt/logger

中文文档
1.1.6发布于 2024/05/17MITTypeScriptESM + CJS近 30 天下载 5628

deno logger available for deno and NPM

原始使用文档
文档目录12 节
  1. Useage
  2. console logger
  3. file and console logger
  4. file logger only
  5. file logger optional parameter
  6. disableConsole and enableConsole
  7. disableFile and enableFile
  8. disable and enable
  9. test
  10. Dual-Stack / Triple-Stack integration
  11. Screenshots
  12. Logger interface

deno-logger

NPM Version JSR Version

Deno / NodeJS colorful logger colorful logger

For Deno usage refer to deno-logger doc

Useage

console logger

import Logger from "@denodnt/logger";

const logger = new Logger();

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

file and console logger

import Logger from "@denodnt/logger";

const logger = new Logger();

// console only
logger.info("i am from consoleLogger", { name: "zfx" });
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.info("i am from fileLogger", { name: "zfx" });
logger.warn("i am from fileLogger", 1, "any");
logger.error("i am from fileLogger", new Error("test"));

file logger only

import Logger from "@denodnt/logger";

const logger = new Logger();
await logger.initFileLogger("../log");
logger.disableConsole();

// file only
logger.info(["i am from fileLogger", 1], { name: "zfx" });

file logger optional parameter

interface

interface fileLoggerOptions {
  rotate?: boolean; // cut by day
  maxBytes?: number;
  // Only available if maxBytes is provided, Otherwise you will get an error
  maxBackupCount?: number;
}

example

import Logger from "@denodnt/logger";
const logger = new Logger();

// cut by day
// filename is [date]_[type].log
// example 2020-05-25_warn.log, 2020-05-25_info.log, 2020-05-25_error.log
await logger.initFileLogger("../log", {
  rotate: true,
});

// maxBytes
// filename is [type].log.[timestamp]
// example: info.log.1590374415956
await logger.initFileLogger("../log", {
  maxBytes: 10 * 1024,
});

// rotate and maxBytes
// filename is [date]_[type].log.[timestamp]
// example: 2020-05-25_info.log.1590374415956
await logger.initFileLogger("../log", {
  rotate: true,
  maxBytes: 10 * 1024,
});

// maxBytes and maxBackupCount
// filename is [type].log.[n]
// example info.log.1, info.log.2 ...
// when reach maxBackupCount, the [type].log.[maxBackupCount-1] will be overwrite
//  detail:
// `maxBytes` specifies the maximum size
// in bytes that the log file can grow to before rolling over to a new one. If the
// size of the new log message plus the current log file size exceeds `maxBytes`
// then a roll over is triggered. When a roll over occurs, before the log message
// is written, the log file is renamed and appended with `.1`. If a `.1` version
// already existed, it would have been renamed `.2` first and so on. The maximum
// number of log files to keep is specified by `maxBackupCount`. After the renames
// are complete the log message is written to the original, now blank, file.
//
// Example: Given `log.txt`, `log.txt.1`, `log.txt.2` and `log.txt.3`, a
// `maxBackupCount` of 3 and a new log message which would cause `log.txt` to
// exceed `maxBytes`, then `log.txt.2` would be renamed to `log.txt.3` (thereby
// discarding the original contents of `log.txt.3` since 3 is the maximum number of
// backups to keep), `log.txt.1` would be renamed to `log.txt.2`, `log.txt` would
// be renamed to `log.txt.1` and finally `log.txt` would be created from scratch
// where the new log message would be written.
await logger.initFileLogger("../log", {
  maxBytes: 10 * 1024,
  maxBackupCount: 10,
});

// rotate and maxBytes and maxBackupCount
// filename is [date]_[type].log.[n]
// example 2020-05-25_info.log.1, 2020-05-25_info.log.2
// when reach maxBackupCount, the [type].log.[maxBackupCount-1] will be overwrite
await logger.initFileLogger("../log", {
  rotate: true,
  maxBytes: 10 * 1024,
  maxBackupCount: 10,
});

// rotate and maxBackupCount
// maxBackupCount will be ignored
await logger.initFileLogger("../log", {
  rotate: true,
  maxBackupCount: 10,
});

The following conditions will throw an error

// maxBackupCount
// get error => maxBackupCount must work with maxBytes
await logger.initFileLogger("../log", {
  maxBackupCount: 10,
});
// rotate and maxBackupCount
// get error => maxBackupCount must work with maxBytes
await logger.initFileLogger("../log", {
  rotate: true,
  maxBackupCount: 10,
});

disableConsole and enableConsole

import Logger from "@denodnt/logger";

const logger = new Logger();

// console
logger.info("console enabled, you can see me");
logger.disableConsole();
// no message is logged
logger.info("console disabled");
logger.enableConsole();
// console
logger.info("console enabled, you can see me");

disableFile and enableFile

import Logger from "@denodnt/logger";

const logger = new Logger();
await logger.initFileLogger("../log");

logger.disableFile();
// not log to file
logger.info("file disbaled");
logger.enableFile();
// log to file
logger.info("file enabled, you can see me");

disable and enable

  • disable disable write to file and terminal, don't care if it is currently writing to a file or terminal, but hope to restore the currently configuration later
  • enable restore previous log configuration: file, terminal or both

example:

  1. fileLogger => disable => enable => fileLogger
  2. consoleLogger => disable => enable => consoleLogger
  3. fileLogger, consoleLogger => disable => enable => fileLogger, consoleLogger
import Logger from "@denodnt/logger";

const logger = new Logger();
await logger.initFileLogger("../log");

logger.disable();
logger.enable();

test

deno test --allow-read --allow-write

Dual-Stack / Triple-Stack integration

A dual-stack project is generaly a npm project that can be import as commonJS module or as ESM module; So if a project can also be import as a Deno module, it's a triple-stack one.

To convert your Deno project to a dual-stack npm project, you should use deno/dnt, then create a _build_npm.ts or scripts/build_npm.ts that looks like:

import { build, emptyDir } from "@deno/dnt";

// grap the next version number as you want
const version: Deno.args[0];
await emptyDir("./npm");
await build({
  entryPoints: ["./mod.ts"],
  outDir: "./npm",
  shims: {
    deno: true,
  },
  compilerOptions: {
    lib: ["dom", "esnext"],
  },
  package: {
    name: "pkg-name",
    version: version,
    // ... package stuff
  },
  // map your favorite deno logger to its npm port.
  mappings: {
    "@denodnt/logger": {
      name: "@denodnt/logger",
      version: "1.1.6",
      peerDependency: false,
    },
  },
});

Screenshots

consoleLogger
consoleLogger

fileLogger
fileLogger

cut logs by day
CutByDay

More screenshots in the screenshots folder.

Logger interface

interface fileLoggerOptions {
  rotate?: boolean;  // cut by day
  maxBytes?: number, // the maximum size in bytes that the log file can grow to before rolling over to a new one
  maxBackupCount?: number // maxBackupCount must work with maxBytes
}

interface LoggerInerface {
  constructor()

  info(...args: unknown[]): void
  warn(...args: unknown[]): void
  error(...args: unknown[]): void

  async initFileLogger(dir: string, options: fileLoggerOptions = {}): Promise<void>

  disableConsole(): void
  enableConsole(): void

  disableFile(): void;
  enableFile(): void;

  disable(): void;
  enable(): void;
}

版本与发布记录

@denodnt/logger 共发布 7 个版本,首次发布于 2023/05/18;过去 12 个月发布了 0 个版本。

版本发布日期距今
1.1.6latest2024/05/172 年前
1.1.52024/02/072 年前
1.1.42024/01/302 年前
1.1.32023/11/093 年前
1.1.22023/05/283 年前
1.1.12023/05/193 年前

关于 @denodnt/logger 的常见问题

@denodnt/logger 是什么?

deno logger available for deno and NPM

如何安装 @denodnt/logger?

在项目目录运行 npm install @denodnt/logger,或使用 pnpm add / yarn add / bun add。

@denodnt/logger 支持 TypeScript 吗?

支持。包内自带 TypeScript 类型声明(package.json 声明了 types 入口),安装后即可获得类型提示,无需再安装 @types 包。

@denodnt/logger 支持 ESM 还是 CommonJS?

同时提供 ES Module 与 CommonJS 入口,import 和 require() 都可以使用,打包工具会自动选择合适的版本。

@denodnt/logger 可以商用吗?

@denodnt/logger 使用 MIT 许可。宽松许可:允许商业使用、修改和再分发,通常只需在分发时保留原版权与许可声明。以上为概括,不构成法律意见,请以许可原文为准。

@denodnt/logger 的下载量有多少?

根据 npm 官方下载统计,@denodnt/logger 在 2026/10/12 – 2026/11/10 这 30 天内被下载 5,628 次,日均约 188 次。下载量会把 CI 构建、镜像同步和作为其他包依赖的安装一并计入,适合横向比较热度,不能直接等同于使用人数。

@denodnt/logger 还在维护吗?

最新版本 1.1.6 发布于 2024/05/17(2 年前);至今共发布 7 个版本,其中过去 12 个月发布了 0 个;首次发布于 2023/05/18。发布频率只是参考信号:功能稳定的工具可能很少需要更新,建议结合源码仓库的 issue 和提交记录判断。