Java基础、中级、高级、架构面试资料

consola 教程

HTML5 herman 5933浏览 0评论
公告:“业余草”微信公众号提供免费CSDN下载服务(只下Java资源),关注业余草微信公众号,添加作者微信:xttblog2,发送下载链接帮助你免费下载!
本博客日IP超过2000,PV 3000 左右,急需赞助商。
极客时间所有课程通过我的二维码购买后返现24元微信红包,请加博主新的微信号:xttblog2,之前的微信号好友位已满,备注:返现
受密码保护的文章请关注“业余草”公众号,回复关键字“0”获得密码
所有面试题(java、前端、数据库、springboot等)一网打尽,请关注文末小程序
视频教程免费领
腾讯云】1核2G5M轻量应用服务器50元首年,高性价比,助您轻松上云

你没看错,consola 和 console 只差一个字母,并且它们都是控制器日志输出的好帮手。console 在某些方面,使用有些局限性。consola 是一个功能更丰富,更漂亮的控制台日志输出控件。今天我们一起来学习它的一些用法。

什么是 Consola ?

  • Easy to use 易于使用。
  • Fancy output with Fallback for Continuous Integration (CI) environments 具有连续集成(CI)环境的备用输出。
  • Global mockable stdout/stderr wrapper 全球mockable stdout/stderr包装。
  • Pluggable reporters  可插拔的插件。
  • Consistent Command Line Interface (CLI) experience 命令行接口(CLI)。
  • Scoped Loggers 有范围的记录日志。

安装 Consola

yarn 安装方法:

yarn add consola

npm 命令安装方法:

npm i consola

基本用法

const consola = require('consola')
// 业余草:www.xttblog.com
// See types section for all available types
consola.start('Starting build')
consola.success('Built!')
consola.info('Reporter: Some info')
consola.error(new Error('Foo'))

图片效果如下:

Consola

输出的日志内容如下:

[2:17:17 PM] Starting build
[2:17:17 PM] [TEST] Log from test scope
[2:17:18 PM] Built!
[2:17:18 PM] Some info
[2:17:18 PM] Error: Foo

日志的作用域

日志的分组使用:

const logger = consola.withScope('test')
logger.info('Log from test scope') // [Test] Log from test scope

Reporter

根据不同的终端,默认情况下使用FancyReporter或BasicReporter。

  • BasicReporter
  • FancyReporter
  • JSONReporter
  • WinstonReporter

上面4个是 Consola 提供的4种可供选择的 Reporter。另外我们还可以自定义 Reporter。相关用法可以查看官方文档:https://github.com/nuxt/consola。更多 API 的用法也可以参考官方 demo 和文档。

业余草公众号

最后,欢迎关注我的个人微信公众号:业余草(yyucao)!可加作者微信号:xttblog2。备注:“1”,添加博主微信拉你进微信群。备注错误不会同意好友申请。再次感谢您的关注!后续有精彩内容会第一时间发给您!原创文章投稿请发送至532009913@qq.com邮箱。商务合作也可添加作者微信进行联系!

本文原文出处:业余草: » consola 教程