@condor-labs/logger

中文文档
3.0.0发布于 2023/03/24MITCommonJS近 30 天下载 3392

此模块为保存 stdout 中的结构化日志提供了有用的帮助.

中文使用文档
文档目录12 节
  1. 兼容性
  2. 怎样用
  3. 实例
  4. 日志错误对象
  5. 特征
  6. 如何出版
  7. 使用 bail 生成 lib 文件
  8. 增加软件包版本
  9. 通过控制台登录到国家预防机制.
  10. 上传新版本
  11. 贡献者
  12. 许可证

此模块为在 stdout 中保存结构化日志提供了有用的帮助. 为后端应用程序推荐的版本大于或等于本模块的 2.x,而非Web客户端. 更多信息见变换组.

结构化的日志对于处理它们非常重要 喷出。 。 。 .

兼容性

从3.x.x版本开始,最小支持的Node.js版本为v14.16.x.

怎样用

要使用图书馆,您只需遵循以下步骤:

  1. 用 npm 安装库
npm install @condor-labs/logger
  1. 导入库 :
const logger = require("@condor-labs/logger");

实例

指数.js

try {
    const logger = require('@condor-labs/logger');
    console.log('-------------------');
    logger.debug({ y: 1, z: 2 });
    console.log('-------------------');
    logger.info({ a: 1, b: 2 });
    console.log('-------------------');
    logger.warn('My text...');
    console.log('-------------------');
    logger.error(Error("my error"));
    console.log('-------------------');
    logger.error('THIS IS SIMPLE TEXT', {name: "my name", country:"Colombia"}, Error("This is an error"));
    console.log('-------------------');
    const arr = [{}, 1, 2];
    arr[0].arr = arr;
    // ⛔️ TypeError: Converting circular structure to JSON
    logger.fatal(arr);
} catch (error) {
    console.error(error);
}

日志错误对象

Error 对象为 特别 需要额外处理的物体。 我们建议这样记录出错的情况:

const logger = require('@condor-labs/logger');

const veryImportantData = { key: '1A2B3C', client: 'my_client' };

try {
    // Runs my code and throws an error
    throw new Error('Unauthorized');
} catch (error) {
    // OK
    logger.error('ERROR_DURING_AUTHORIZATION', error, { veryImportantData });
    // NOT RECOMMENDED
    logger.error('ERROR_DURING_AUTHORIZATION', { error, veryImportantData });
}

输出( 此输出格式化为读取目的) 。 在运行时间,它将被打印为一行JSON:

/*
OK:
{
  "level": "ERROR",
  "time": 1676986542037,
  "pid": 29461,
  "hostname": "MyPC.local",
  "arg1": "ERROR_DURING_AUTHORIZATION",
  "arg2": {
    "message": "Unauthorized",
    "stack": "Error: Unauthorized
        at Object.<anonymous> (/.../index.js:30:11)
        at Module._compile (node:internal/modules/cjs/loader:1155:14)
        at Object.Module._extensions..js (node:internal/modules/cjs/loader:1209:10)
        at Module.load (node:internal/modules/cjs/loader:1033:32)
        at Function.Module._load (node:internal/modules/cjs/loader:868:12)
        at Function.executeUserEntryPoint [as runMain] (node:internal/modules/run_main:81:12)
        at node:internal/main/run_main_module:22:47"
  },
  "arg3": { "veryImportantData": { "key": "1A2B3C", "client": "my_client" } }
}

NOT RECOMMENDED WAY:
{
  "level": "ERROR",
  "time": 1676986691805,
  "pid": 29631,
  "hostname": "MyPC.local",
  "arg1": "ERROR_DURING_AUTHORIZATION",
  "arg2": {
    "error": {},
    "veryImportantData": { "key": "1A2B3C", "client": "my_client" }
  }
}
*/

注意到 error 内 arg2 被打印为空对象.

特征

  • 设置您的掩体 LOGGER_DEBUG_MODE = 1 显示调试消息( e) logger.debug({ y: 1, z: 2 });否则,那些信息不会出现在控制台.
  • 导入 loggerMiddleware 从日志中将其设置为您的 ExpressJS 服务器的中间软件:
const logger = require('@condor-labs/logger');
const { loggerMiddleware } = logger;
...
app.use(loggerMiddleware());

此中间软件是显示 pino-http 模块 用于基于ExpressJS的服务器. 请检查docs的更多信息.

如何出版

使用 bail 生成 lib 文件

一旦您在文件夹的文件中完成贡献 src/logger/运行 npm run build。这将在文件夹中生成您更改的流出版本 lib/,其中包含导出为日志库的文件。 然后,移动到下一步 出版.

增加软件包版本

你需要更新 package.json 放在根文件夹中的文件.

识别财产 version 并增加正确的数字 + 1.

通过控制台登录到国家预防机制.

 npm login
 [Enter username]
 [Enter password]
 [Enter email]

如果一切都好,控制台会告诉你这样的东西: Logged in as USERNAME on https://registry.npmjs.org/.

上传新版本

 npm publish --access public

参考: https://docs.npmjs.com/getting-started/publishing-npm-packages 页面存档备份,存于互联网档案馆

Note: you will need to have a NPM account, if you don't have one create one here: https://www.npmjs.com/signup

贡献者

此模块的原作者和当前主要维护者是 @condor-labs 开发团队。 。 。 .

更多关于神鹰实验室 给。 。 。 .

许可证

麻省理工学院

版本与发布记录

@condor-labs/logger 共发布 14 个版本,首次发布于 2019/06/28;过去 12 个月发布了 0 个版本。

版本发布日期距今
3.0.0latest2023/03/243 年前
2.0.22023/03/233 年前
2.0.12023/02/233 年前
2.0.02023/02/103 年前
1.4.0-rc.12023/02/093 年前
1.3.02022/08/314 年前

关于 @condor-labs/logger 的常见问题

@condor-labs/logger 是什么?

此模块为保存 stdout 中的结构化日志提供了有用的帮助.

如何安装 @condor-labs/logger?

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

@condor-labs/logger 支持 TypeScript 吗?

该包的 package.json 没有声明类型入口。需要类型提示时,可以在 npm 上查找社区维护的 @types/condor-labs__logger 是否存在。

@condor-labs/logger 支持 ESM 还是 CommonJS?

以 CommonJS 形式发布,可以直接 require();在 ES Module 项目中也可以用 import 引入(Node.js 会把 module.exports 作为默认导出)。

@condor-labs/logger 需要什么版本的 Node.js?

package.json 的 engines 字段要求 Node.js >=14.6.0。版本不符时 npm 默认只给出警告,开启 engine-strict 后会拒绝安装。

@condor-labs/logger 可以商用吗?

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

@condor-labs/logger 的下载量有多少?

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

@condor-labs/logger 还在维护吗?

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