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 🙏

© 2026 – Pkg Stats / Ryan Hefner

@micl/logger

v0.2.4

Published

micl logger

Readme

@micl/logger

npm version npm downloads license

一个轻量级的日志管理工具,同时支持 Node.js 和浏览器环境,支持多日志级别和模块特定日志。

✨ 特性

  1. 支持多日志级别(info、warn、error)
  2. 模块特定日志管理
  3. 自动创建日志目录(Node.js 环境)
  4. 日志文件按级别分类存储(Node.js 环境)
  5. 控制台和文件双重输出(Node.js 环境)
  6. 浏览器环境自动降级为控制台输出
  7. 完整的 TypeScript 类型支持
  8. 单例模式,全局统一管理

📦 安装

npm install @micl/logger
# or
pnpm add @micl/logger
# or
yarn add @micl/logger

🎯 快速使用

import logger from '@micl/logger';

// 初始化日志配置
logger.init({
  rootPath: './server-logs',
  modules: ['auth', 'database'],
  levels: ['info', 'warn', 'error'],
});

// 通用日志
logger.getLogger().info('Server started');
logger.getLogger().warn('Memory usage is high');
logger.getLogger().error('Failed to connect to database');

// 模块特定日志
logger.getLogger('auth').info('User login successful');
logger.getLogger('database').error('Connection lost');

浏览器环境使用

在浏览器环境中使用时,无需配置 rootPath,日志仅会输出到控制台:

import logger from '@micl/logger';

// 初始化日志配置(浏览器环境)
logger.init({
  modules: ['auth', 'database'],
  levels: ['info', 'warn', 'error'],
});

// 通用日志
logger.getLogger().info('Page loaded');
logger.getLogger().warn('Deprecated API used');
logger.getLogger().error('Something went wrong');

// 模块特定日志
logger.getLogger('auth').info('User logged in');

📚 模块导出

默认导出 LoggerClass 单例实例。

🛠 API 参考

logger.init(config)

初始化日志配置。

配置选项:

| 选项 | 类型 | 默认值 | 描述 | |------|------|--------|------| | rootPath | string | './logs' | 日志文件存储根目录(浏览器环境自动忽略) | | modules | string[] | [] | 需要创建的模块日志列表 | | prefix | string | '' | 日志前缀 | | levels | string[] | ['info', 'warn', 'error'] | 支持的日志级别 |

logger.init({
  rootPath: './server-logs',
  modules: ['auth', 'database'],
  levels: ['info', 'warn', 'error'],
});

logger.getLogger(module?)

获取日志对象引用,可传入模块名获取指定模块的日志实例。

// 获取根日志实例
const log = logger.getLogger();

// 获取指定模块的日志实例
const authLog = logger.getLogger('auth');
authLog.info('User login successful');
authLog.error('Failed to login');

info(...args)

输出 info 级别日志。

logger.getLogger().info('Server started', { port: 3000 });

warn(...args)

输出 warn 级别日志。

logger.getLogger().warn('Memory usage is high', { usage: '85%' });

error(...args)

输出 error 级别日志。

logger.getLogger().error('Database connection failed', new Error('Connection timeout'));

模块日志

初始化后可通过模块名访问模块日志:

import logger from '@micl/logger';

logger.init({
  modules: ['auth', 'database'],
});

logger.getLogger('auth').info('User login successful');
logger.getLogger('database').error('Connection lost');

📝 日志输出格式

每条日志记录包含时间戳和日志级别:

[INFO][2024-01-15 10:30:45] Server started
[WARN][2024-01-15 10:30:46] Memory usage is high
[ERROR][2024-01-15 10:30:47] Database connection failed

🤝 贡献

欢迎提交 Issue 和 Pull Request 来完善这个模块。

📄 许可证

本项目采用 MIT 许可证 - 查看 LICENSE 文件了解详情。

Copyright (c) alexgogoing [email protected]

📞 支持

如有问题或建议,请提交 Issue 或联系维护者。


@micl/logger - 跨环境的日志管理工具(Node.js & 浏览器)🚀