V2EX = way to explore
V2EX 是一个关于分享和探索的地方
现在注册
已注册用户请  登录
turing
V2EX  ›  Node.js

consoler 打印色彩丰富的console.log

  •  
  •   turing · 2013-08-31 18:25:23 +08:00 · 11118 次点击
    这是一个创建于 3882 天前的主题,其中的信息可能已经有所发展或是发生改变。


    经常会写许多cli工具,每个工具都会多少输出一些提示信息在console,有一些信息是每个模块都要加的,比如package名,当前版本,通常还有成功,正在处理中,处理完成,失败等等情况,所以把常用到的东西做成了cosnoler这个包,使用起来也非常简单:

    consoler.log('success','hey dude, well done !');

    如果想要添加自己的颜色组,可以在colors.json这个文件里新增,也可以在程序中动态添加

    consoler.add('alldone','green');
    consoler.log('alldone','all done !!!!!!!');

    除此之外,这个工具会自动去读你项目的文件版本,不用传进去。

    Github:
    https://github.com/turingou/consoler
    22 条回复    1970-01-01 08:00:00 +08:00
    liaa
        1
    liaa  
       2013-08-31 18:29:30 +08:00
    consoler.log('success','hey dude, well done !');
    zhangxiao
        2
    zhangxiao  
       2013-08-31 18:30:37 +08:00
    支持
    一般需要比较好的log的时候,我就装winston了
    turing
        3
    turing  
    OP
       2013-08-31 18:31:57 +08:00
    @zhangxiao winston功能强大多啦,依赖也多~
    kfll
        4
    kfll  
       2013-08-31 18:58:05 +08:00
    在用 bunyan
    orzfly
        5
    orzfly  
       2013-08-31 20:27:25 +08:00
    其实我有个疑问,这种彩色 log 有办法保存下来么……然后再看的时候有像 less 那样的工具么
    fanzeyi
        6
    fanzeyi  
       2013-08-31 20:44:48 +08:00
    https://github.com/Marak/colors.js

    这个项目在 npmjs.org 的 Most Depended 栏中排第八。
    turing
        7
    turing  
    OP
       2013-08-31 21:56:36 +08:00
    @fanzeyi 恩,这个就是依赖colors的,我标题可能写的不太清楚,consoler不是用来生成彩色文字,只是用一些helper打印出常用的log头尾,给console.log的一个enhancement
    deepure
        8
    deepure  
       2013-08-31 23:13:38 +08:00   ❤️ 1
    哈哈,楼主你又挖坑了
    hfcorriez
        9
    hfcorriez  
       2013-08-31 23:45:40 +08:00
    建议把Label对齐,可以又比较好的日志阅读体验
    hfcorriez
        10
    hfcorriez  
       2013-08-31 23:46:32 +08:00
    可以直接consoler.success(), consoler.error() 这样更简单
    turing
        11
    turing  
    OP
       2013-09-01 01:43:47 +08:00
    @hfcorriez 之前是那样想的,但考虑要自己定义新的,这样api实现起来不太方便。。。对齐这个,因为label也是自己定义的,估计也不太好做吧。。我想想
    menway
        12
    menway  
       2013-09-01 11:28:16 +08:00
    @turing 对齐可以考虑用\t
    hfcorriez
        13
    hfcorriez  
       2013-09-03 17:39:14 +08:00
    @turing 对齐可以考虑最大Label长度限制,不够就padding空格,我是这么做的。Label搞太长有点变态。自定义API,只要按照一些规范来定制,还是可以接受的,最起码看起来和写起来都会简单。
    kfll
        14
    kfll  
       2013-09-03 21:37:53 +08:00   ❤️ 1
    @orzfly
    用 bunyan 输出的日志文件,内容是可读不带颜色的,但是如果通过管道传给 bunyan cli 就能输出有颜色的日志了。
    假设 bunyan 输出一个日志文件叫 bootstrap.log,执行:
    cat bootstrap.log | node_modules/.bin/bunyan
    就是彩色的了
    orzfly
        15
    orzfly  
       2013-09-03 23:54:17 +08:00
    @kfll 好东西,非常感谢指教。
    lovejoy
        16
    lovejoy  
       2013-09-04 00:02:44 +08:00
    @kfll 是根据关键字加了个颜色?
    kfll
        17
    kfll  
       2013-09-04 00:12:16 +08:00
    @lovejoy bunyan 的输出是格式化的(一行一JSON)
    turing
        18
    turing  
    OP
       2013-09-05 12:23:18 +08:00
    @hfcorriez consoler ship to 0.0.4

    支持了快捷方式: consoler.error('error'), consoler.success('gotcha');

    支持了对齐,默认是不足8个字符长度补足,也可以自己通过 consoler.align(NUMBER) 来设定。

    截图:

    hfcorriez
        19
    hfcorriez  
       2013-09-17 17:19:14 +08:00
    @turing 赞,可以试试居中可能更好点。
    xieren58
        20
    xieren58  
       2013-09-17 17:27:19 +08:00
    对齐更难看~
    turing
        21
    turing  
    OP
       2013-09-17 17:31:28 +08:00
    @xieren58 恩,所以新版里把 [] 删掉了
    timothyye
        22
    timothyye  
       2013-11-07 12:44:43 +08:00
    不错,github上已star
    关于   ·   帮助文档   ·   博客   ·   API   ·   FAQ   ·   我们的愿景   ·   实用小工具   ·   5697 人在线   最高记录 6543   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 40ms · UTC 06:28 · PVG 14:28 · LAX 23:28 · JFK 02:28
    Developed with CodeLauncher
    ♥ Do have faith in what you're doing.