Files
FastGPT/sdk/logger
Archer aaa7d17ef1 V4.14.9 dev (#6555)
* feat: encapsulate logger (#6535)

* feat: encapsulate logger

* update engines

---------

Co-authored-by: archer <545436317@qq.com>

* next config

* dev shell

* Agent sandbox (#6532)

* docs: switch to docs layout and apply black theme (#6533)

* feat: add Gemini 3.1 models

- Add gemini-3.1-pro-preview (released February 19, 2026)
- Add gemini-3.1-flash-lite-preview (released March 3, 2026)

Both models support:
- 1M context window
- 64k max response
- Vision
- Tool choice

* docs: switch to docs layout and apply black theme

- Change layout from notebook to docs
- Update logo to icon + text format
- Apply fumadocs black theme
- Simplify global.css (keep only navbar and TOC styles)
- Fix icon components to properly accept className props
- Add mobile text overflow handling
- Update Node engine requirement to >=20.x

* doc

* doc

* lock

* fix: ts

* doc

* doc

---------

Co-authored-by: archer <archer@archerdeMac-mini.local>
Co-authored-by: archer <545436317@qq.com>

* Doc (#6493)

* cloud doc

* doc refactor

* doc move

* seo

* remove doc

* yml

* doc

* fix: tsconfig

* fix: tsconfig

* sandbox version (#6497)

* sandbox version

* add sandbox log

* update lock

* fix

* fix: sandbox

* doc

* add console

* i18n

* sandbxo in agent

* feat: agent sandbox

* lock

* feat: sandbox ui

* sandbox check exists

* env tempalte

* doc

* lock

* sandbox in chat window

* sandbox entry

* fix: test

* rename var

* sandbox config tip

* update sandbox lifecircle

* update prompt

* rename provider test

* sandbox logger

* yml

---------

Co-authored-by: Archer <archer@fastgpt.io>
Co-authored-by: archer <archer@archerdeMac-mini.local>

* perf: sandbox error tip

* Add sandbox limit and fix some issue (#6550)

* sandbox in plan

* fix: some issue

* fix: test

* editor default path

* fix: comment

* perf: sandbox worksapce

* doc

* perf: del sandbox

* sandbox build

* fix: test

* fix: pr comment

---------

Co-authored-by: Ryo <whoeverimf5@gmail.com>
Co-authored-by: Archer <archer@fastgpt.io>
Co-authored-by: archer <archer@archerdeMac-mini.local>
2026-03-16 17:09:25 +08:00
..
2026-03-16 17:09:25 +08:00
2026-03-16 17:09:25 +08:00
2026-03-16 17:09:25 +08:00
2026-03-16 17:09:25 +08:00
2026-03-16 17:09:25 +08:00

@fastgpt-sdk/logger

FastGPT 的通用日志 SDK基于 @logtape/logtape,内置:

  • Console pretty log
  • OpenTelemetry OTLP log sink
  • AsyncLocalStorage 上下文透传
  • 兼容 FastGPT 现有 getLogger(...).info('msg', {...}) 调用风格

安装

pnpm add @fastgpt-sdk/logger

快速开始

import { configureLogger, getLogger, withContext } from '@fastgpt-sdk/logger';

await configureLogger({
  defaultCategory: ['my-app'],
  console: {
    enabled: true,
    level: 'info'
  },
  otel: {
    enabled: true,
    serviceName: 'my-app',
    url: 'http://localhost:4318/v1/logs',
    level: 'info'
  },
  sensitiveProperties: ['password', 'token']
});

const logger = getLogger(['my-app', 'worker']);

await withContext({ requestId: 'req_123' }, async () => {
  logger.info('worker started', { jobId: 'job_001' });
});

API

configureLogger(options)

type LoggerConfigureOptions = {
  defaultCategory?: readonly string[];
  console?: boolean | { enabled?: boolean; level?: LogLevel };
  otel?: false | {
    enabled?: boolean;
    level?: LogLevel;
    serviceName: string;
    url?: string;
    loggerName?: string;
  };
  sensitiveProperties?: readonly string[];
  contextLocalStorage?: AsyncLocalStorage<Record<string, unknown>>;
  loggers?: Config['loggers'];
};
  • defaultCategory: 默认 category不传时默认 ['system']
  • console: 控制台输出配置
  • otel: OpenTelemetry 输出配置;启用时需要 serviceName
  • sensitiveProperties: 命中这些字段的日志不会发往 OTEL sink
  • contextLocalStorage: 可注入自己的上下文存储实例
  • loggers: 可覆盖默认的 LogTape logger 路由配置

getLogger(category?)

const logger = getLogger(['app', 'api']);

logger.info('request completed', { status: 200 });
logger.error('request failed', { error });

category 不需要预注册;传任意字符串数组即可。

withContext(context, fn)

await withContext({ requestId: 'req_123' }, async () => {
  logger.info('handling request');
});

Env Helper

如果项目已经使用环境变量管理日志配置,也可以直接用 SDK 提供的转换助手:

import { configureLoggerFromEnv } from '@fastgpt-sdk/logger';

await configureLoggerFromEnv({
  env: process.env,
  defaultCategory: ['my-app'],
  defaultServiceName: 'my-app',
  sensitiveProperties: ['password', 'token']
});

支持读取这些环境变量:

  • LOG_ENABLE_CONSOLE
  • LOG_CONSOLE_LEVEL
  • LOG_ENABLE_OTEL
  • LOG_OTEL_LEVEL
  • LOG_OTEL_SERVICE_NAME
  • LOG_OTEL_URL

OpenTelemetry

启用 otelSDK 会创建 OTLP HTTP log exporter。

await configureLogger({
  otel: {
    enabled: true,
    serviceName: 'my-app',
    loggerName: 'my-app',
    url: 'http://localhost:4318/v1/logs'
  }
});

也可以直接从 @fastgpt-sdk/logger 导入 getOpenTelemetrySink 进行更细粒度集成。

FastGPT 迁移建议

如果你在 FastGPT 仓库内部使用:

  • 通用能力放到 sdk/logger
  • 业务分类常量继续放在 service 侧
  • service 侧保留一层 configureLoggerFromEnv 风格的兼容封装