diff options
author | Anton Tananaev <anton.tananaev@gmail.com> | 2013-07-30 20:42:02 +1200 |
---|---|---|
committer | Anton Tananaev <anton.tananaev@gmail.com> | 2013-07-30 20:42:02 +1200 |
commit | 110c49504f87c025423c923500bd52ea4615b33b (patch) | |
tree | 2398fd3fa3286b35b6f80cad51b59703d3065b6e /src/org/traccar/helper | |
parent | 5f75f109dfc1140d2f644857d877baf88cb1fe83 (diff) | |
download | traccar-server-110c49504f87c025423c923500bd52ea4615b33b.tar.gz traccar-server-110c49504f87c025423c923500bd52ea4615b33b.tar.bz2 traccar-server-110c49504f87c025423c923500bd52ea4615b33b.zip |
Improve logging system (fix #278)
Diffstat (limited to 'src/org/traccar/helper')
-rw-r--r-- | src/org/traccar/helper/Log.java | 90 | ||||
-rw-r--r-- | src/org/traccar/helper/NamedParameterStatement.java | 2 |
2 files changed, 63 insertions, 29 deletions
diff --git a/src/org/traccar/helper/Log.java b/src/org/traccar/helper/Log.java index 983171b72..4cd61b933 100644 --- a/src/org/traccar/helper/Log.java +++ b/src/org/traccar/helper/Log.java @@ -1,5 +1,5 @@ /* - * Copyright 2012 Anton Tananaev (anton.tananaev@gmail.com) + * Copyright 2012 - 2013 Anton Tananaev (anton.tananaev@gmail.com) * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -15,53 +15,87 @@ */ package org.traccar.helper; -import java.util.logging.Level; -import java.util.logging.Logger; +import java.io.IOException; +import java.util.Properties; +import org.apache.log4j.Appender; +import org.apache.log4j.DailyRollingFileAppender; +import org.apache.log4j.Layout; +import org.apache.log4j.Level; +import org.apache.log4j.LogManager; +import org.apache.log4j.Logger; +import org.apache.log4j.PatternLayout; -/** - * Logger - */ public class Log { - private static final String TRACCAR_LOGGER_NAME = "traccar"; - private static Logger logger = null; + + public static void setupLogger(Properties properties) throws IOException { + if (Boolean.valueOf(properties.getProperty("logger.enable"))) { + + Layout layout = new PatternLayout( + "%d{yyyy-MM-dd HH:mm:ss} %5p: %m%n"); + + Appender appender = new DailyRollingFileAppender( + layout, properties.getProperty("logger.file"), "'.'yyyyMMdd"); + + LogManager.resetConfiguration(); + logger = Logger.getRootLogger(); + logger.addAppender(appender); + logger.setLevel(Level.DEBUG); + } + } - /** - * Return global logger - */ public static Logger getLogger() { if (logger == null) { - logger = Logger.getLogger(TRACCAR_LOGGER_NAME); - logger.setUseParentHandlers(false); - logger.setLevel(Level.ALL); + logger = Logger.getRootLogger(); + logger.setLevel(Level.OFF); } return logger; } - private static void write(Level level, String msg) { - StackTraceElement[] stack = Thread.currentThread().getStackTrace(); - if (stack != null && stack.length > 3) { - getLogger().logp(level, stack[3].getClassName(), stack[3].getMethodName(), msg); - } else { - getLogger().log(level, msg); - } + public static void error(String msg) { + getLogger().error(msg); } - public static void severe(String msg) { - write(Level.SEVERE, msg); + public static void warning(String msg) { + getLogger().warn(msg); } - public static void warning(String msg) { - write(Level.WARNING, msg); + public static void warning(Throwable exception) { + warning(null, exception); + } + + public static void warning(String msg, Throwable exception) { + StringBuilder s = new StringBuilder(); + if (msg != null) { + s.append(msg); + s.append(" - "); + } + if (exception != null) { + String exceptionMsg = exception.getMessage(); + if (exceptionMsg != null) { + s.append(exceptionMsg); + s.append(" - "); + } + s.append(exception.getClass().getName()); + StackTraceElement[] stack = exception.getStackTrace(); + if (stack.length > 0) { + s.append(" ("); + s.append(stack[0].getFileName()); + s.append(":"); + s.append(stack[0].getLineNumber()); + s.append(")"); + } + } + getLogger().warn(s.toString()); } public static void info(String msg) { - write(Level.INFO, msg); + getLogger().info(msg); } - public static void fine(String msg) { - write(Level.FINE, msg); + public static void debug(String msg) { + getLogger().debug(msg); } } diff --git a/src/org/traccar/helper/NamedParameterStatement.java b/src/org/traccar/helper/NamedParameterStatement.java index eb5c411f2..833f4aea3 100644 --- a/src/org/traccar/helper/NamedParameterStatement.java +++ b/src/org/traccar/helper/NamedParameterStatement.java @@ -132,7 +132,7 @@ public class NamedParameterStatement { try { reset(true); } catch (SQLException secondError) { - Log.warning("Exception during query preparation: " + secondError.getMessage()); + Log.warning(secondError); throw secondError; } } |