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

beautify-console-log

v1.3.16

Published

This is a further beautification and encapsulation of the 'console' object. You can use it like using "console. log", "console. info", "console. warn", "console. error", and it can display the code line information where the log is printed.

Downloads

1,984

Readme

beautify-console-log

介绍

由于大部分日志美化插件都不能定位到代码所在行,所以我自己实现了这个库。

目录

效果展示

安装教程

npm i beautify-console-log --save

yarn add beautify-console-log

使用说明

  1. 简单使用
  • 为了方便使用(也为了兼容老版本),我把参数定义为多种类型,比如使用config配置时,可以传入type值为:LogType类型,也可以传入字符串"info""log""warn""error"
import BeautifyConsole from "beautify-console-log";
const log = BeautifyConsole.getInstance();
// 使用方式与正常的console.info()一致
log.info(1234, '4', [3, 5]);

import BeautifyConsole from "beautify-console-log";
const log = new BeautifyConsole();
log.info(111111);

const log = new BeautifyConsole();
// 使用方式与正常的console.info()一致
log.info(1234, '4', [3, 5]);

也可以直接使用 dist/index.js 文件

<script src="./dist/index.js">
<script>
  const log = BeautifyConsole.default.getInstance()
  log.info(1234, '4', [3, 5])
  // 也可以这样
  const log2 = new BeautifyConsole.default()
  log2.error(111)
</script>
const log = BeautifyConsole.default.getInstance()

log.info(1234, '4', [3, 5])

log.log(1234)

log.close().warn('no show')

log.open().log('show log')

log.error(1234)

log.setPadStartText({
    title: "hello world ->",
    logType: LogType.info,
}).log(1234)

console.log('------------------------------------------------')
console.log('--------Set log header text and style-----------')

log.setPadStartText('log', 'hello world:', {
    color: 'red',
    bgColor: 'green'
}).log(1234)
log.log(1)

console.log('------------------------------------------------')
console.log('----------------config start--------------------')


log.config({
    title: 'custom title',
    type: ['info', 'error']
})

log.warn('custom warn', 'no show')

log.info('custom log')

log.error('custom error')

console.log('------------------------------------------------')
console.log('----------------config reset--------------------')

log.reset()
log.info('reset log')
log.warn('reset warn')

如果想在生成环境关闭多余日志,可以这么配置:

import BeautifyConsole from "beautify-console-log";
import { LogType } from 'beautify-console-log/lib/beautify-console/model';
const log = BeautifyConsole.getInstance();

/**
 * vite 使用 import.meta.env.MODE 获取环境
 */
log.config({
    title: 'custom title',
    type: process.env.NODE_ENV === 'product' ? ['error'] : ['log', 'info', 'warn', 'error']
})
log.info(1234, '4', [3, 5]); // 不会显示打印

或者

import BeautifyConsole from "beautify-console-log";
import { LogType } from 'beautify-console-log/lib/beautify-console/model';
const log = BeautifyConsole.getInstance();

/**
 * vite 使用 import.meta.env.MODE 获取环境
 */
if (process.env.NODE_ENV === 'product') {
  log.close().open(LogType.error);
}
log.info(1234, '4', [3, 5]); // 不会显示打印
  1. 初始化配置
const log = BeautifyConsole.getInstance();
import { LogType } from 'beautify-console-log/lib/beautify-console/model';
log.config({
    title: 'example pad start text', // 左侧填充的日志头内容
    type: [LogType.info, LogType.error, LogType.warn, LogType.log], // 显示部分日志类型
})
log.info(1234, '4', [3, 5]);
log.log(1234, '4', [3, 5]);
log.warn(1234, '4', [3, 5]);
log.error(1234, '4', [3, 5]);
  1. 支持的console类型
const log = BeautifyConsole.getInstance();
log.info(1234, '4', [3, 5]);
log.log(1234, '4', [3, 5]);
log.warn(1234, '4', [3, 5]);
log.error(1234, '4', [3, 5]);
  1. 加入自定义console日志头
const log = BeautifyConsole.getInstance();
log.setPadStartText({
    title: "hello world ->",
    logType: LogType.info,
}).info(1234,'4 ', [3, 5])
// 也可以这样
log.setPadStartText({
    title: "hello world ->",
    logType: LogType.info,
}).info(1234,'4 ', [3, 5]).info(1234, '4', [3, 5]);
  1. 关闭日志,传入参数就关闭对应的console日志类型,不传就关闭所有的类型,支持链式调用
// ...省略
const log = BeautifyConsole.getInstance();
log.close(LogType.info);
log.close(LogType.log);
log.close(LogType.warn);
log.close(LogType.error);
log.close();
log.close().open(LogType.error);

// or
log.open(LogType.error).open(LogType.log).open(LogType.warn).open(LogType.info);

// or
log.close(LogType.error).info('closed error');
log.close(LogType.error).error('closed error');

// or
log.close(LogType.error).open(LogType.info);
log.close(LogType.error).open(LogType.info).info('info...');
  1. 打开日志,传入参数就打开对应的console日志类型,不传就打开所有的类型,支持链式调用
const log = BeautifyConsole.getInstance();
log.open(LogType.info);
log.open(LogType.log);
log.open(LogType.warn);
log.open(LogType.error);
log.open();
log.open().close(LogType.info);

//or
log.open(LogType.error).open(LogType.log).open(LogType.warn).open(LogType.info);

// or
log.open().info('closed error');
log.open(LogType.error).error('closed error');

// or
log.close(LogType.error).open(LogType.info);
log.close(LogType.error).open(LogType.info).info('info...');

API

config

|参数名 |值类型 |描述 | |-------------------------------|-----------------------------|-----------------------------| |param|BaseConfig|| |├──title |String? |自定义日志头,值为空时不显示"title" | |└──type |LogType[] | ('info' 、 'log' 、 'warn' 、 'error')[] |显示的日志类型,设置后只显示对应的日志类型(LogType.infoLogType.logLogType.warnLogType.error"info""log""warn""error")|

import BeautifyConsole from "beautify-console-log";
import { LogType } from 'beautify-console-log/lib/beautify-console/model';
const log = BeautifyConsole.getInstance();
log.config({
    title: 'custom title',
    type: [LogType.info, LogType.error, 'log']
})
// 使用方式与正常的console.info()一致
log.info(1234, '4', [3, 5]);

// 或者
const log2 = new BeautifyConsole();
log2.info(111111);

log

使用方式与正常的console.log()一致

import BeautifyConsole from "beautify-console-log";
const log = BeautifyConsole.getInstance();
log.log(1234, '4', [3, 5]);
log.log({
    "name": "chengzan"
});

info

使用方式与正常的console.info()一致

import BeautifyConsole from "beautify-console-log";
const log = BeautifyConsole.getInstance();
log.info(1234, '4', [3, 5]);
log.info({
    "name": "chengzan"
});

warn

使用方式与正常的console.warn()一致

import BeautifyConsole from "beautify-console-log";
const log = BeautifyConsole.getInstance();
log.warn(1234, '4', [3, 5]);
log.warn('warn');

error

使用方式与正常的console.error()一致

import BeautifyConsole from "beautify-console-log";
const log = BeautifyConsole.getInstance();
log.error(1234, '4', [3, 5]);
log.error('warn');

open

使用log.close()关闭日志后,可以log.open()进行打开对应的日志类型,打开所有类型日志时,不传参(支持链式调用)。 |值类型 |描述 | |-----------------------------|-----------------------------| |LogType | "info" | "log" | "warn" | "error" |LogType.infoLogType.logLogType.warnLogType.error,或者不传|

import BeautifyConsole from "beautify-console-log";
import { LogType } from 'beautify-console-log/lib/beautify-console/model';
const log = BeautifyConsole.getInstance();
log.open() // 打开所有类型日志
// 或者
log.open(LogType.info) // 打开info日志
// 或者
log.open(LogType.info).open('error') // 打开info日志

close

关闭日志,可以关闭全部日志或者某个类型的日志。 |值类型 |描述 | |-----------------------------|-----------------------------| |LogType | "info" | "log" | "warn" | "error" |LogType.infoLogType.logLogType.warnLogType.error"info""log""warn""error",或者不传|

import BeautifyConsole from "beautify-console-log";
import { LogType } from 'beautify-console-log/lib/beautify-console/model';
const log = BeautifyConsole.getInstance();
log.close() // 关闭所有类型日志
// 或者
log.close(LogType.info) // 关闭info日志
// 或者
log.close(LogType.info).open('log')

setPadStartText

设置日志头文本内容及文本样式 |参数名 |值类型 |描述 | |-------------------------------|-----------------------------|-----------------------------| |param|PadStartText|| |├──title |String |自定义日志头 | |├──logType | LogType | "info" | "log" | "warn" | "error" |LogType.info,LogType.log,LogType.warn,LogType.error"info""log""warn""error"| |└──style |PadStartStyle || | |├──color? (ColorType |、 'black' | 'red' | 'green' | 'yellow' | 'blue' | 'purple' | 'cyan' | 'white') |ColorType.black,ColorType.red,ColorType.green,ColorType.yellow,ColorType.blue,ColorType.purple,ColorType.cyan,ColorType.white| | |└──bgColor? (ColorType | 'black' | 'red' | 'green' | 'yellow' | 'blue' | 'purple' | 'cyan' | 'white') |ColorType.black,ColorType.red,ColorType.green,ColorType.yellow,ColorType.blue,ColorType.purple,ColorType.cyan,ColorType.white|

import BeautifyConsole from "beautify-console-log";
import { LogType, ColorType } from 'beautify-console-log/lib/beautify-console/model';
const log = BeautifyConsole.getInstance();
log.close() // 关闭所有类型日志
// 或者
log.close(LogType.info) // 关闭info日志
// 或者
log.close(LogType.info).open('log')

log.setPadStartText({
    title: "hello world ->",
    logType: LogType.info,
    style: {
      color: ColorType.yellow,
      bgColor: ColorType.purple,
    }
}).log(1234)
// 或者
log.setPadStartText({
    title: "hello world ->",
    logType: LogType.info,
    style: {
      color: 'black',
      bgColor: 'purple',
    }
}).log(1234)

reset

当设置自定义日志头或关闭部分日志等操作后,可以通过log.reset()重置。

import BeautifyConsole from "beautify-console-log";
import { LogType } from 'beautify-console-log/lib/beautify-console/model';
const log = BeautifyConsole.getInstance();

log.config({
    title: 'custom title',
    type: [LogType.info, LogType.error]
})

log.reset() // 打开所有类型日志
log.info('reset log')

Types 类型定义

BaseConfig

{
  type?: LogType[] | ('info' | 'log' | 'warn' | 'error')[]
  title?: string 
}

BaseColorType

{
  color?: ColorType;
  bgColor?: ColorType;
}

PadStartStyle

{
  color?: ColorType;
  bgColor?: ColorType;
}

PadStartText

{
  title: string;
  logType: LogType | 'info' | 'log' | 'warn' | 'error';
  style?: PadStartStyle;
}

ColorType

{
  black = 90,
  red = 91,
  green = 92,
  yellow = 93,
  blue = 94,
  purple = 95,
  cyan = 96,
  white = 97,
}

LogType

{
  info = "info",
  warn = "warn",
  error = "error",
  log = "log",
}

Utils

formatConsoleStr

import BeautifyConsole from "beautify-console-log";
import { formatConsoleStr } from 'beautify-console-log/lib/utils';

// node.js
log.info(formatConsoleStr('string=%s number=%d', 'string', 1).join(''))
// browser
log.info(...formatConsoleStr('string=%s number=%d', 'string', 1))

参与贡献

  1. Fork 本仓库
  2. 新建 Feat_xxx 分支
  3. 提交代码
  4. 新建 Pull Request