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

@yomua/y-tlog

v0.1.6

Published

用于终端和浏览器打印彩色日志

Readme

描述

用于终端或浏览器打印彩色日志并记录

示例

基本使用

import log from '@yomua/t-log'

/**
 * 打印: 
    1 2 3 4 5
 * 日志记录: 
    [2024/1/22 13:52:45] [INFO] 1 2 3 4 5
 */
log(1, 2, 3, 4, 5)

/**
 * 打印: 
    { name: 'yomua' } [ 1, 2 ]
 * 日志记录: 
    [2024/1/26 17:47:05] [INFO] {"name":"yomua"} 1 2
 */
log({ name: 'yomua' }, [1, 2])

/**
 * 打印: 
    Parent
      parent_message
      son
        son_message
        grandSon
          grandSon_message
 * 日志记录: 
    [2024/1/22 11:43:31] [GROUP] Parent
      "parent_message"
    [2024/1/22 11:43:31] [GROUP] son
      "son_message"
    [2024/1/22 11:43:31] [GROUP] grandSon
      "grandSon_message"
 */
log.group('Parent', [
  {
    message: 'parent_message',
    type: 'error',
  },
  {
    message: {
      title: 'son',
      sub: [
        { message: 'son_message' },
        {
          message: {
            title: 'grandSon',
            sub: [{ message: 'grandSon_message' }],
          },
          type: 'group',
        },
      ],
    },
    type: 'group',
  },
])

/**
 * 打印: 
  ┌─────────┬─────────┬──────┐
  │ (index) │  name   │ age  │
  ├─────────┼─────────┼──────┤
  │    0    │ 'yomua' │ '18' │
  └─────────┴─────────┴──────┘
 * 日志记录:
    [2024/1/22 14:21:43] [TABLE] {"name":"yomua","age":"18"}
 */
log.table([{ name: 'yomua', age: '18' }])

/**
 * 打印: 
    [ { name: 'yomua' } ]
 * 日志记录: 
    [2024/1/22 14:22:33] [DIR] [{"name":"yomua"}]
 */
log.dir([{ name: 'yomua' }])

/**
 * 打印: 
    error
 * 日志记录: 
    [2024/1/24 16:52:59] [ERROR] error
 */
log.error('error')

/**
 * 打印: 
    success
 * 日志记录: 
    [2024/1/24 16:52:59] [SUCCESS] success
 */
log.success('success')

/**
 * 打印: 
    warn1 warn2
 * 日志记录: 
    [2024/1/22 14:22:33] [WARN]
      warn1
      warn2
 */
log.warn('warn1', 'warn2')

染色

import log from '@yomua/t-log'

const { dye } = log
dye.info('yomua', 'To') // yomuaTo (白色)
dye.success('yomua', 'To') // yomuaTo (绿色)
dye.error('yomua', 'To') // yomuaTo (红色)
dye.warning('yomua', 'To') // yomuaTo (黄色)

// 16 进制
log(dye.hex('#ffffff')('yomua')) // yomua (白色)
log(dye.bgHex('#fff')('yomua')) // yomua (背景白色)

// rgb 色彩
log(dye.rgb(255, 0, 0)('yomua')) // yomua (红色)
log(dye.bgRgb(255, 0, 0)('yomua')) // yomua (背景红色)

风格

import log from '@yomua/t-log'

const { style } = log
log(style.bold('yomua')) // yomua (加粗)
log(style.italic('yomua')) // yomua (斜体)

//... 更多风格参见 style 类型导出

写入磁盘

只有在 node 环境下才有用, 对于 browser 环境, 是无效的, 什么都不会发生.

import log from '@yomua/t-log'

// 必须: 启用 write - 默认 false
log.writeConfig.isWrite = true //

// 可选: 写入的路径 - 默认 ./
log.writeConfig.filePath = './'

// 可选: 写入的文件名 - 默认 y-tlog
log.writeConfig.filename = 'y-tlog'

// 可选: 写入的文件后缀 - 默认 log
log.writeConfig.fileSuffix = 'log'

// 任何一个 log 都会将数据写入本地, 默认以时间降序
log('yomua')
log.group('title', [{ message: 'yomua' }])
log.dir([{ name: 'yomua' }])

发送请求

node 环境使用 http.request()

browser 环境使用 fetch()

import log from '@yomua/t-log'

// 必须: 启用请求
log.requestConfig.isRequest = true

// 必须: 配置服务地址 - 默认 ''
log.requestConfig.url = 'http://localhost:4000/test'

// 可选: http.RequestOptions  - 默认 undefined
log.requestConfig.options = {
  method: 'POST',
  headers: {
    'Content-Type': 'text/plain',
  },
}

// 可选: 错误回调  - 默认 () => null
log.requestConfig.onError = (error) => {}

// 可选: 成功回调  - 默认 () => null
// 可以通过 onReceiver 返回的数据来决定 data
log.requestConfig.onSuccess = (data) => {
  // 回调函数中不允许再次调用 log, 否则会造成无限循环 -> 因为每次 log 都会发送请求, 同时再触发 onSuccess;
  // 如果一定要怎么做, 可以添加:
  // => log.requestConfig.isRequest = false
  // => 设置成不允许请求, 就不会继续发送请求
  console.dir(data)
}

// 可选: 数据传输时回调 - 针对 node  - 默认 () => null
// 当服务端实现流式传输, 则此回调也将在每传输一次数据时就调用
log.requestConfig.onReceiver = (chunk) => {
  return '_onReceiver' + chunk
}

// 任何一个 log 都将发送请求
log('yomua')
log.group('title', [{ message: 'yomua' }])
log.dir([{ name: 'yomua' }])

对于发送请求操作, 携带的数据为原始数据, 即: log 时使用什么数据, 就发送什么类型数据.

特殊情况: 当传入多个参数给 log 时, 会将这些参数组装成一个数组发送.

// 发送 [1, 2, 3, 4]
log(1, 2, 3, 4)

// 发送 {name: 'yomua'}
log.dir({ name: 'yomua' })

chalk?

你知道的, 较新版本的 chalk (5.3.0) 目前是不支持 CommonJS 的, 所以如果对于有使用到 CommonJS 的项目来说, chalk 使用起来并不方便.

@yomua/y-tlog 已将 chalk 内置, 并且可使用 log.chalk 逃生到 chalk, 同时也支持 CommonJS.

FAQ

log.group 时多次发送请求

是的, 目前 log.group 实现会进行递归, 每次递归都会发送请求

无限循环发送请求

检查 onSuccess, onError, onReceiver 中是否继续调用了 log;

如果是, 请删除;

或者使用:

log.requestConfig.onSuccess = (data) => {
  log.requestConfig.isRequest = false
  log.dir(data)
}