V2EX = way to explore
V2EX 是一个关于分享和探索的地方
Sign Up Now
For Existing Member  Sign In
turing
V2EX  ›  Node.js

consoler 打印色彩丰富的console.log

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

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

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

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

    截图:

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