npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2025 – Pkg Stats / Ryan Hefner

@coffic/cosy-logger

v0.2.3

Published

Cozy Framework 的日志系统

Readme

Cosy Logger

Cosy Logger 是一个为 Electron 应用程序设计的灵活且可扩展的日志框架。它的设计灵感来自 Laravel 的日志系统,基于 electron-log 构建,通过服务提供者、通道和驱动程序提供结构化的日志管理方式。

特性

  • 服务提供者集成:与 cosy-framework 服务容器无缝集成
  • 多通道日志:支持配置多个日志通道(如 apperrorplugin),每个通道可以有不同的设置
  • 基于驱动的架构:易于扩展自定义日志驱动
  • 文件日志:在生产环境中将日志持久化到文件系统
  • 控制台日志:在开发过程中提供彩色格式化的控制台输出
  • 可配置性:支持通过应用程序配置文件自定义日志行为

安装

pnpm add @coffic/cosy-logger

基本使用

import { Log } from '@coffic/cosy-logger';

// 使用默认通道记录日志
Log.info('这是一条信息日志');
Log.error('发生了一个错误', { error: new Error('错误详情') });

// 使用特定通道记录日志
Log.channel('plugin').info('插件已启动');
Log.channel('security').warn('检测到可疑活动');

配置

默认配置

Cosy Logger 提供了一套默认配置,包括常用的日志通道和设置。默认配置包括:

{
  default: 'app',  // 默认通道
  channels: {
    app: {
      driver: 'electron',
      level: 'info',
      format: 'structured',
      includeTimestamp: false
    },
    error: {
      driver: 'electron',
      level: 'error',
      format: 'json'
    },
    plugin: {
      driver: 'electron',
      level: 'info',
      format: 'structured',
      includeTimestamp: false
    }
    // ... 其他预配置通道
  }
}

自定义配置

你可以通过应用程序的配置系统自定义日志行为。在你的应用配置文件中添加 logger 配置:

// config/logger.ts
export default {
  default: 'app', // 修改默认通道
  channels: {
    app: {
      driver: 'electron',
      level: 'debug', // 覆盖默认级别
      format: 'json', // 修改输出格式
      includeTimestamp: true, // 添加时间戳
    },
    customChannel: {
      // 添加新的通道
      driver: 'electron',
      level: 'info',
      format: 'simple',
    },
  },
};

配置选项说明:

  • default: 设置默认的日志通道
  • channels: 定义日志通道配置
    • driver: 日志驱动类型(目前支持 'electron' 和 'stack')
    • level: 日志级别('debug', 'info', 'warn', 'error')
    • format: 输出格式('simple', 'structured', 'json')
    • includeTimestamp: 是否包含时间戳
    • channels: (仅用于 stack 驱动)要组合的通道列表

类型支持

为了确保类型安全,我们提供了 LoggerConfig 接口:

import { LoggerConfig } from '@coffic/cosy-logger';

const config: LoggerConfig = {
  default: 'app',
  channels: {
    // ... 你的通道配置
  },
};

禁用日志通道

为了方便关闭某个日志通道,用户可以在日志配置文件中将对应通道的 driver 值设置为 null。当 driver 设置为 null 时,该日志通道将被禁用,所有与该通道相关的日志调用都不会产生任何输出。

这种方式为在不同环境(例如生产环境)下灵活控制日志记录提供了一种简单直观的手段,而无需修改代码,只需调整配置即可。

日志文件位置

在生产环境中,日志不仅会输出到控制台,还会写入到用户系统的文件中。日志文件存储在每个操作系统的标准应用程序日志目录中:

  • macOS: ~/Library/Logs/{appName}/
  • Windows: %USERPROFILE%\AppData\Roaming\{appName}\logs\
  • Linux: ~/.config/{appName}/logs/

在应用程序中访问日志

你可以使用 Electron 的 shell 模块在应用程序中打开日志目录:

import { app, shell } from 'electron';

const logsPath = app.getPath('logs');
shell.openPath(logsPath);

高级用法

使用 Stack 驱动

Stack 驱动允许你将多个日志通道组合在一起:

{
  channels: {
    production: {
      driver: 'stack',
      channels: ['app', 'error']  // 同时写入到 app 和 error 通道
    }
  }
}

添加自定义驱动

你可以通过 LogManagerextend 方法添加自定义驱动:

const manager = app.make<LogManagerContract>('log.manager');

manager.extend('custom-file', (config) => {
  // 返回你的自定义日志通道实现
  return {
    log: (level, message, context) => {
      // 实现你的日志记录逻辑
    },
  };
});

贡献

欢迎提交 Issue 和 Pull Request 来帮助改进这个项目。