<i id='oVnen'><tr id='oVnen'><dt id='oVnen'><q id='oVnen'><span id='oVnen'><b id='oVnen'><form id='oVnen'><ins id='oVnen'></ins><ul id='oVnen'></ul><sub id='oVnen'></sub></form><legend id='oVnen'></legend><bdo id='oVnen'><pre id='oVnen'><center id='oVnen'></center></pre></bdo></b><th id='oVnen'></th></span></q></dt></tr></i><div id='oVnen'><tfoot id='oVnen'></tfoot><dl id='oVnen'><fieldset id='oVnen'></fieldset></dl></div>

        <tfoot id='oVnen'></tfoot>

        <small id='oVnen'></small><noframes id='oVnen'>

          <bdo id='oVnen'></bdo><ul id='oVnen'></ul>

        <legend id='oVnen'><style id='oVnen'><dir id='oVnen'><q id='oVnen'></q></dir></style></legend>
      1. 在 Eclipse 中更改 java.util.logging.Logger 输出的颜色和格式

        Change color and format of java.util.logging.Logger output in Eclipse(在 Eclipse 中更改 java.util.logging.Logger 输出的颜色和格式)
      2. <tfoot id='WLls6'></tfoot>

        1. <legend id='WLls6'><style id='WLls6'><dir id='WLls6'><q id='WLls6'></q></dir></style></legend>
        2. <i id='WLls6'><tr id='WLls6'><dt id='WLls6'><q id='WLls6'><span id='WLls6'><b id='WLls6'><form id='WLls6'><ins id='WLls6'></ins><ul id='WLls6'></ul><sub id='WLls6'></sub></form><legend id='WLls6'></legend><bdo id='WLls6'><pre id='WLls6'><center id='WLls6'></center></pre></bdo></b><th id='WLls6'></th></span></q></dt></tr></i><div id='WLls6'><tfoot id='WLls6'></tfoot><dl id='WLls6'><fieldset id='WLls6'></fieldset></dl></div>

          <small id='WLls6'></small><noframes id='WLls6'>

            <tbody id='WLls6'></tbody>
              <bdo id='WLls6'></bdo><ul id='WLls6'></ul>
                  本文介绍了在 Eclipse 中更改 java.util.logging.Logger 输出的颜色和格式的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

                  问题描述

                  我正在寻找一种方法来更改 Eclipse 中 java.util.logging.Logger 的日志输出颜色.由于我还没有真正找到解决改变颜色并将其与 Logger 类相结合的解决方案,因此我想在此处记录我的解决方案.

                  I was looking for a way to change the color of a log output from java.util.logging.Logger in Eclipse. Since I haven't really found a solution that addresses changing color and combining that with the Logger class I would like to document my solution here.

                  推荐答案

                  新建一个继承自 Formatter 的类

                  Create a new class which inherits from Formatter

                  import java.text.SimpleDateFormat;
                  import java.util.Date;
                  import java.util.logging.Formatter;
                  import java.util.logging.LogRecord;
                  
                  public class LogFormatter extends Formatter
                  {
                      // ANSI escape code
                      public static final String ANSI_RESET = "u001B[0m";
                      public static final String ANSI_BLACK = "u001B[30m";
                      public static final String ANSI_RED = "u001B[31m";
                      public static final String ANSI_GREEN = "u001B[32m";
                      public static final String ANSI_YELLOW = "u001B[33m";
                      public static final String ANSI_BLUE = "u001B[34m";
                      public static final String ANSI_PURPLE = "u001B[35m";
                      public static final String ANSI_CYAN = "u001B[36m";
                      public static final String ANSI_WHITE = "u001B[37m";
                  
                      // Here you can configure the format of the output and 
                      // its color by using the ANSI escape codes defined above.
                  
                      // format is called for every console log message
                      @Override
                      public String format(LogRecord record)
                      {
                          // This example will print date/time, class, and log level in yellow,
                          // followed by the log message and it's parameters in white .
                          StringBuilder builder = new StringBuilder();
                          builder.append(ANSI_YELLOW);
                  
                          builder.append("[");
                          builder.append(calcDate(record.getMillis()));
                          builder.append("]");
                  
                          builder.append(" [");
                          builder.append(record.getSourceClassName());
                          builder.append("]");
                  
                          builder.append(" [");
                          builder.append(record.getLevel().getName());
                          builder.append("]");
                  
                          builder.append(ANSI_WHITE);
                          builder.append(" - ");
                          builder.append(record.getMessage());
                  
                          Object[] params = record.getParameters();
                  
                          if (params != null)
                          {
                              builder.append("	");
                              for (int i = 0; i < params.length; i++)
                              {
                                  builder.append(params[i]);
                                  if (i < params.length - 1)
                                      builder.append(", ");
                              }
                          }
                  
                          builder.append(ANSI_RESET);
                          builder.append("
                  ");
                          return builder.toString();
                      }
                  
                      private String calcDate(long millisecs) {
                          SimpleDateFormat date_format = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
                          Date resultdate = new Date(millisecs);
                          return date_format.format(resultdate);
                      }
                  }
                  


                  您可以像这样将自定义格式化程序绑定到您的记录器:


                  You can bind the custom formatter to your logger like this:

                  Logger logger = Logger.getLogger("logfile.txt");
                  logger.setUseParentHandlers(false);
                  
                  ConsoleHandler handler = new ConsoleHandler();
                  
                  Formatter formatter = new LogFormatter();
                  handler.setFormatter(formatter);        
                  
                  logger.addHandler(handler);
                  

                  可以使用 这个插件

                  来源:

                  • http://www.vogella.com/tutorials/Logging/article.html

                  如何在控制台打印颜色使用 System.out.println?

                  https://mihai-nita.net/2013/06/03/eclipse-plugin-ansi-in-console/

                  https://docs.oracle.com/javase/8/docs/api/java/util/logging/Logger.html#setLevel-java.util.logging.Level-

                  https://docs.oracle.com/javase/8/docs/api/java/util/logging/Level.html

                  一个尊重 ANSI 颜色的 Eclipse 控制台视图代码?

                  这篇关于在 Eclipse 中更改 java.util.logging.Logger 输出的颜色和格式的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

                  本站部分内容来源互联网,如果有图片或者内容侵犯了您的权益,请联系我们,我们会在确认后第一时间进行删除!

                  相关文档推荐

                  How to send data to COM PORT using JAVA?(如何使用 JAVA 向 COM PORT 发送数据?)
                  How to make a report page direction to change to quot;rtlquot;?(如何使报表页面方向更改为“rtl?)
                  Use cyrillic .properties file in eclipse project(在 Eclipse 项目中使用西里尔文 .properties 文件)
                  Is there any way to detect an RTL language in Java?(有没有办法在 Java 中检测 RTL 语言?)
                  How to load resource bundle messages from DB in Java?(如何在 Java 中从 DB 加载资源包消息?)
                  How do I change the default locale settings in Java to make them consistent?(如何更改 Java 中的默认语言环境设置以使其保持一致?)

                  <small id='nnK3r'></small><noframes id='nnK3r'>

                  <legend id='nnK3r'><style id='nnK3r'><dir id='nnK3r'><q id='nnK3r'></q></dir></style></legend>
                    • <bdo id='nnK3r'></bdo><ul id='nnK3r'></ul>

                        <tfoot id='nnK3r'></tfoot>

                            <tbody id='nnK3r'></tbody>
                        1. <i id='nnK3r'><tr id='nnK3r'><dt id='nnK3r'><q id='nnK3r'><span id='nnK3r'><b id='nnK3r'><form id='nnK3r'><ins id='nnK3r'></ins><ul id='nnK3r'></ul><sub id='nnK3r'></sub></form><legend id='nnK3r'></legend><bdo id='nnK3r'><pre id='nnK3r'><center id='nnK3r'></center></pre></bdo></b><th id='nnK3r'></th></span></q></dt></tr></i><div id='nnK3r'><tfoot id='nnK3r'></tfoot><dl id='nnK3r'><fieldset id='nnK3r'></fieldset></dl></div>