The Logger in Java and Android

來源:互聯網
上載者:User

轉載: http://whotodo.iteye.com/blog/1701596

在物件導向的世界裡,我們可以將對象擬人化。它們不僅擁有資源(資料),還能實現一些功能(方法)。Logger就是Java內建的,Android也支援的一個可以將log資訊寫到控制台或檔案的log資訊記錄員。

    package org.vhow.android;            import java.io.IOException;      import java.util.logging.ConsoleHandler;      import java.util.logging.FileHandler;      import java.util.logging.Level;      import java.util.logging.Logger;            public class AppMain {                public static void main(String[] args) throws SecurityException,                  IOException {              // Create a Logger whose name is "AppMain"              Logger aLogger = Logger.getLogger(AppMain.class.getName());                    // This Logger object will records all kinds of logs.              aLogger.setLevel(Level.ALL);                    // Write logging message to Console.              sentLogMessageToConsole(aLogger);                    // Writer logging message to a File.              sentLogMessageToFile(aLogger);                    // Log a message.              aLogger.log(Level.INFO, "info");                }                private static void sentLogMessageToConsole(Logger aLogger) {              // A ConsoleHandler object can publish records to System.err.              ConsoleHandler aConsoleHandler = new ConsoleHandler();                    // All kinds of logs will be logged by this handler.              aConsoleHandler.setLevel(Level.ALL);                    // Format LogRecords for this Handler.              aConsoleHandler.setFormatter(new LogRecordFormatter());                    // Add the ConsoleHanler object to receive the logging messages.              aLogger.addHandler(aConsoleHandler);          }                private static void sentLogMessageToFile(Logger aLogger) throws IOException {              // A FileHandler object can writer log message to a file.              FileHandler aFileHandler = new FileHandler("logs.log");                    // All kinds of logs will be logged by this handler              aFileHandler.setLevel(Level.ALL);                    // / Add the FileHandler object to receive the logging messages.              aLogger.addHandler(aFileHandler);          }            }  

我們還需要一個定義log record格式的類

    package org.vhow.android;            import java.text.SimpleDateFormat;      import java.util.Date;      import java.util.logging.Formatter;      import java.util.logging.LogRecord;            class LogRecordFormatter extends Formatter {          SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd hh:mm:ss");          Date date;                @Override          public String format(LogRecord record) {              date = new Date(record.getMillis());              return dateFormat.format(date) + ", " + record.getSourceClassName()                      + ", " + record.getSourceMethodName() + ", "                      + record.getLevel() + ": " + record.getMessage() + "\n";          }            }  

如果我們不規定log資訊的格式,則預設的打到控制台的log格式如下:

預設寫到檔案中的log格式如下:

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.