Java Tutorial/Log/Log Formatter
Содержание
Create custom formatter
import java.util.logging.ConsoleHandler;
import java.util.logging.Formatter;
import java.util.logging.Handler;
import java.util.logging.LogRecord;
import java.util.logging.Logger;
public class MainClass {
private static Logger logger = Logger.getLogger("SimpleFormatterExample");
private static void logMessages() {
logger.info("Line One");
logger.info("Line Two");
}
public static void main(String[] args) {
logger.setUseParentHandlers(false);
Handler conHdlr = new ConsoleHandler();
conHdlr.setFormatter(new Formatter() {
public String format(LogRecord record) {
return record.getLevel() + " : " + record.getSourceClassName() + " -:- "
+ record.getSourceMethodName() + " -:- " + record.getMessage() + "\n";
}
});
logger.addHandler(conHdlr);
logMessages();
}
}
Creating a Custom Formatter for a Logger Handler
import java.util.Date;
import java.util.logging.FileHandler;
import java.util.logging.Formatter;
import java.util.logging.Handler;
import java.util.logging.Level;
import java.util.logging.LogRecord;
import java.util.logging.Logger;
class MyHtmlFormatter extends Formatter {
public String format(LogRecord rec) {
StringBuffer buf = new StringBuffer(1000);
if (rec.getLevel().intValue() >= Level.WARNING.intValue()) {
buf.append("<b>");
buf.append(rec.getLevel());
buf.append("</b>");
} else {
buf.append(rec.getLevel());
}
buf.append(" ");
buf.append(rec.getMillis());
buf.append(" ");
buf.append(formatMessage(rec));
buf.append("\n");
return buf.toString();
}
public String getHead(Handler h) {
return "<HTML><HEAD>" + (new Date()) + "</HEAD><BODY><PRE>\n";
}
public String getTail(Handler h) {
return "</PRE></BODY></HTML>\n";
}
}
public class Main {
public static void main(String[] argv) throws Exception {
Logger logger = Logger.getLogger("com.mycompany");
FileHandler fh = new FileHandler("mylog.html");
fh.setFormatter(new MyHtmlFormatter());
logger.addHandler(fh);
logger.setLevel(Level.ALL);
logger.severe("my severe message");
logger.info("my info message");
logger.entering("Main class", "myMethod", new Object[] { "para1", "para2" });
}
}
Define your own Custom Formatter
import java.util.logging.Formatter;
import java.util.logging.LogRecord;
public class CustomFormatter extends Formatter {
public synchronized String format(LogRecord record) {
String methodName = record.getSourceMethodName();
String message = record.getMessage();
StringBuffer buffer = new StringBuffer(50);
buffer.append(methodName);
buffer.append("=");
buffer.append(message);
return buffer.toString();
}
}
Logger with XMLFormatter and FileHandler
import java.util.logging.FileHandler;
import java.util.logging.Handler;
import java.util.logging.Logger;
import java.util.logging.XMLFormatter;
public class Main {
public static void main(String[] args) throws Exception {
Logger logger = Logger.getLogger("my.log");
Handler handler = null;
handler = new FileHandler("messages.log");
logger.addHandler(handler);
handler.setFormatter(new XMLFormatter());
logger.info("Our first logging message");
logger.severe("Something terrible happened");
}
}
Log HTML formatter
import java.util.Date;
import java.util.logging.FileHandler;
import java.util.logging.Handler;
import java.util.logging.Level;
import java.util.logging.LogManager;
import java.util.logging.LogRecord;
import java.util.logging.Logger;
import java.util.logging.XMLFormatter;
class HTMLFormatter extends java.util.logging.Formatter {
public String format(LogRecord record) {
return ("<tr><td>" + (new Date(record.getMillis())).toString() + "</td><td>"
+ record.getMessage() + "</td></tr>\n");
}
public String getHead(Handler h) {
return ("<html>\n <body>\n" + "<Table border>\n<tr><td>Time</td><td>Log Message</td></tr>\n");
}
public String getTail(Handler h) {
return ("</table>\n</body>\n</html>");
}
}
public class LoggingHTMLFormatter {
public static void main(String args[]) throws Exception {
LogManager lm = LogManager.getLogManager();
Logger parentLogger, childLogger;
FileHandler xml_handler = new FileHandler("log_output.xml");
FileHandler html_handler = new FileHandler("log_output.html");
parentLogger = Logger.getLogger("ParentLogger");
childLogger = Logger.getLogger("ParentLogger.ChildLogger");
lm.addLogger(parentLogger);
lm.addLogger(childLogger);
parentLogger.setLevel(Level.WARNING);
childLogger.setLevel(Level.ALL);
xml_handler.setFormatter(new XMLFormatter());
html_handler.setFormatter(new HTMLFormatter());
parentLogger.addHandler(xml_handler);
childLogger.addHandler(html_handler);
childLogger.log(Level.FINE, "This is a fine log message");
childLogger.log(Level.SEVERE, "This is a severe log message");
xml_handler.close();
html_handler.close();
}
}
Set log Formatter
import java.util.logging.FileHandler;
import java.util.logging.Logger;
import java.util.logging.SimpleFormatter;
public class MainClass {
private static Logger logger = Logger.getLogger("LogToFile2");
public static void main(String[] args) throws Exception {
FileHandler logFile = new FileHandler("LogToFile2.txt");
logFile.setFormatter(new SimpleFormatter());
logger.addHandler(logFile);
logger.info("A message logged to the file");
}
}
Setting the Formatter of a Logger Handler
import java.util.logging.FileHandler;
import java.util.logging.Logger;
import java.util.logging.SimpleFormatter;
public class Main{
public static void main(String[] argv) throws Exception{
Logger logger = Logger.getLogger("com.mycompany");
FileHandler fh = new FileHandler("mylog.txt");
fh.setFormatter(new SimpleFormatter());
logger.addHandler(fh);
// fh = new FileHandler("mylog.xml");
// fh.setFormatter(new XMLFormatter());
// logger.addHandler(fh);
// Log a few messages
logger.severe("my severe message");
logger.warning("my warning message");
logger.info("my info message");
logger.config("my config message");
logger.fine("my fine message");
logger.finer("my finer message");
logger.finest("my finest message");
}
}
XMLFormatter based Logging
import java.util.logging.FileHandler;
import java.util.logging.Level;
import java.util.logging.LogRecord;
import java.util.logging.XMLFormatter;
public class Main {
public static void main(String args[]) throws Exception{
XMLFormatter formatter = new XMLFormatter();
LogRecord record = new LogRecord(Level.INFO, "XML message..");
FileHandler handler = new FileHandler("newxml.xml");
handler.setFormatter(formatter);
handler.publish(record);
handler.flush();
}
}