控制台输出彩色log

阿甘 11月前 1032

记录一下,可以借鉴 python 的 loguru 库

Code AardioLine:65复制
  • 1.
  • 2.
  • 3.
  • 4.
  • 5.
  • 6.
  • 7.
  • 8.
  • 9.
  • 10.
  • 11.
  • 12.
  • 13.
  • 14.
  • 15.
  • 16.
  • 17.
  • 18.
  • 19.
  • 20.
  • 21.
  • 22.
  • 23.
  • 24.
  • 25.
  • 26.
  • 27.
  • 28.
  • 29.
  • 30.
  • 31.
  • 32.
  • 33.
  • 34.
  • 35.
  • 36.
  • 37.
  • 38.
  • 39.
  • 40.
  • 41.
  • 42.
  • 43.
  • 44.
  • 45.
  • 46.
  • 47.
  • 48.
  • 49.
  • 50.
  • 51.
  • 52.
  • 53.
  • 54.
  • 55.
  • 56.
  • 57.
  • 58.
  • 59.
  • 60.
  • 61.
  • 62.
  • 63.
  • 64.
  • 65.
    • namespace logger
    • import console;
    • import debug;
    • import preg;
    • class Logger{
    • ctor(){
    • ..console.open();
    • this.queryinfo = "select source,function,upvars,name,currentline,activelines";
    • this.regex = ..preg("(?<=//).+(?=\.\.\.)");
    • this.type_enum = {
    • [1] = "INFO ";
    • [2] = "DEBUG ";
    • [3] = "WARNING";
    • [4] = "ERROR ";
    • [5] = "SUCCESS";
    • }
    • this.type = 1;
    • this.color = ..console.color.white;
    • };
    • logMessage = function(msg, dInfo){
    • var _time = tostring(..time());
    • //var _file = this.regex.match(dInfo["source"]["src"]);
    • var _line = dInfo["currentline"];
    • var _function = dInfo["name"] == null ? "module" : dInfo["name"];
    • ..console.writeColorText(_time, ..console.color.darkGreen);
    • ..console.writeColorText(" | ");
    • ..console.writeColorText(this.type_enum[this.type], this.color);
    • ..console.writeColorText(" | ");
    • ..console.writeColorText(_function++":"++_line, ..console.color.cyan);
    • ..console.writeColorText(' - ');
    • ..console.writeColorText(msg++'\n', this.color);
    • }
    • info = function(msg){
    • this.type = 1;
    • this.color = ..console.color.white;
    • var dInfo = ..debug.queryinfo(2, this.queryinfo);
    • this.logMessage(msg, dInfo);
    • }
    • debug = function(msg){
    • this.type = 2;
    • this.color = ..console.color.blue;
    • var dInfo = ..debug.queryinfo(2, this.queryinfo);
    • this.logMessage(msg, dInfo);
    • }
    • warning = function(msg){
    • this.type = 3;
    • this.color = ..console.color.darkGray;
    • var dInfo = ..debug.queryinfo(2, this.queryinfo);
    • this.logMessage(msg, dInfo);
    • }
    • error = function(msg){
    • this.type = 4;
    • this.color = ..console.color.red;
    • var dInfo = ..debug.queryinfo(2, this.queryinfo);
    • this.logMessage(msg, dInfo);
    • }
    • success = function(msg){
    • this.type = 5;
    • this.color = ..console.color.green;
    • var dInfo = ..debug.queryinfo(2, this.queryinfo);
    • this.logMessage(msg, dInfo);
    • }
    • }
    Code AardioLine:14复制
  • 1.
  • 2.
  • 3.
  • 4.
  • 5.
  • 6.
  • 7.
  • 8.
  • 9.
  • 10.
  • 11.
  • 12.
  • 13.
  • 14.
    • //test.aardio
    • import logger;
    • logger = logger.Logger();
    • var testFunc = function(){
    • logger.info("Hello world!");
    • logger.debug("Hello world!");
    • logger.warning("Hello world!");
    • logger.error("Hello world!");
    • logger.success("Hello world!");
    • }
    • testFunc();
    • console.askYesNo("按Y键继续,按N键取消");


    最新回复 (2)
    • 光庆 11月前
      0 2

      好东西,顶


    • kio 11月前
      0 3
      漂亮
    返回