turing
V2EX  ›  Node.js

consoler 打印色彩丰富的console.log

  •  
  •   turing · Aug 31, 2013 · 12723 views
    This topic created in 4815 days ago, the information mentioned may be changed or developed.


    经常会写许多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 replies  •  1970-01-01 08:00:00 +08:00
    liaa
    liaa
    Aug 31, 2013
    1
    consoler.log('success','hey dude, well done !');
    zhangxiao
    zhangxiao
    Aug 31, 2013
    2
    支持
    一般需要比较好的log的时候,我就装winston了
    turing
    turing
    OP
    Aug 31, 2013
    3
    @zhangxiao winston功能强大多啦,依赖也多~
    kfll
    kfll
    Aug 31, 2013
    4
    在用 bunyan
    orzfly
    orzfly
    Aug 31, 2013
    5
    其实我有个疑问,这种彩色 log 有办法保存下来么……然后再看的时候有像 less 那样的工具么
    fanzeyi
    fanzeyi
    Aug 31, 2013
    6
    https://github.com/Marak/colors.js

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

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

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

    截图:

    hfcorriez
    hfcorriez
    Sep 17, 2013
    19
    @turing 赞,可以试试居中可能更好点。
    xieren58
    xieren58
    Sep 17, 2013
    20
    对齐更难看~
    turing
    turing
    OP
    Sep 17, 2013
    21
    @xieren58 恩,所以新版里把 [] 删掉了
    timothyye
    timothyye
    Nov 7, 2013
    22
    不错,github上已star
    About   ·   Help   ·   Advertise   ·   Blog   ·   API   ·   FAQ   ·   Privacy   ·   Solana   ·   1847 Online   Highest 6679   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 58ms · UTC 01:10 · PVG 09:10 · LAX 17:10 · JFK 20:10
    ♥ Do have faith in what you're doing.