📄 serverloggingmanager.java
字号:
/**
* $Log: ServerLoggingManager.java,v $
* Revision 1.1 2003/01/16 13:08:09 mwulff
* initial version
*
* Revision 1.3 2002/12/31 12:19:39 mwulff
* no message
*
* Revision 1.2 2002/12/23 13:03:28 mwulff
* no message
*
* Revision 1.1 2002/12/20 18:49:02 mwulff
* no message
*
* Revision 1.8 2002/12/14 22:41:57 mwulff
* no message
*
* Revision 1.7 2002/12/13 20:06:16 mwulff
* no message
*
* Revision 1.6 2002/12/01 21:38:10 mwulff
* is now implemented as singleton
*
* Revision 1.5 2002/11/25 11:09:08 mwulff
* changed the default configuration of the server logging system
*
* Revision 1.4 2002/11/21 11:11:20 mwulff
* code reorganisation
*
* Revision 1.3 2002/11/17 17:50:36 mwulff
* the field logConfPath must now be set by another class (JKFBroker) and is
* nolonger retrieved from ServerResourceManager
*
* Revision 1.2 2002/11/15 18:28:44 mwulff
* added default logging configuration if a logging config can't be loaded
*
* Revision 1.1 2002/11/07 16:25:17 mwulff
* initial version
*
*/
package de.fhm.jkf.resource.sv;
import java.io.File;
import org.apache.log4j.DailyRollingFileAppender;
import org.apache.log4j.Layout;
import org.apache.log4j.Level;
import org.apache.log4j.Logger;
import org.apache.log4j.PatternLayout;
import org.apache.log4j.xml.DOMConfigurator;
/**
* @author marten wulff
*
* <br><br><center><table border="1" width="80%"><hr>
* <strong><a href="http://jkf.sourceforge.net">The JKF Project</a></strong>
* <p>
* Copyright (C) 2002 by Marten Wulff
* <p>
* This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation; either
* version 2.1 of the License, or (at your option) any later version.
* <p>
* This library is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Lesser General Public License for more details.
* <p>
* You should have received a copy of the <a href="http://www.gnu.org/copyleft/lesser.html">
* GNU Lesser General Public License</a> along with this library; if not, write to
* the Free Software Foundation, Inc., 59 Temple Place, Suite 330, Boston,
* MA 02111-1307 USA
* <hr></table></center>
*/
public class ServerLoggingManager {
// path for the file that contains the server logging
// configuration
private String logConfPath = null;
private static ServerLoggingManager instance = null;
private static Object semaphore = new Object();
// Logger and Appender variables
private Logger jkfServerLogger = Logger.getLogger("de.fhm.jkf");
private DailyRollingFileAppender jkfServerAppender = null;
/* Default Levels for logging.
* localLogLevel == Level for the jkf logger
* localAppenderLevel == Level for the jkf DailyRollingFileAppender
* All these variables are not declared final, because it
* should be able to change the values dynamically from
* the server application.
*/
private Level jkfLogLevel = Level.DEBUG;
// Default name of the log file.
private final String DEFAULT_LOG_FILENAME = "jkfserver.log";
// The layout that is used for logging messages.
private final String DEFAULT_LOG_PATTERN =
"[%t] %-5p (%F:%L) - %m\n";
// DatePattern that is used for a DailyRollingFileAppender
private final String LOGFILE_DATE_PATTERN = "'.'yyyy-MM-dd";
Layout defaultLayout = null;
// Flag that indicates if ServerLoggingManager is already
// configured
private boolean configured = false;
public static ServerLoggingManager instance() {
if(instance == null) {
synchronized(semaphore) {
if(instance == null) {
instance = new ServerLoggingManager();
}
}
}
return instance;
}
/**
* Constructor for ServerLoggingManager. This class
* should't be instantiated
*/
private ServerLoggingManager() {
super();
}
public synchronized void configure() {
// don't configure twice
if (configured) {
return;
}
// Define a default layout, that is used for client loggers.
// If not config file for logging exists, the default layout
// is used for the jkf logger too.
defaultLayout = new PatternLayout(DEFAULT_LOG_PATTERN);
boolean fileExists = false;
/* Let's see if the specified configuration file is exists.
* If we call DOMConfigurator.configure(fileName) with a
* non existing file, the log4j system will produce an
* exception, which isn't thrown and therefore we cannot
* detect if an error occured initialising the log4j system.
*/
if (logConfPath != null) {
File f = new File(logConfPath);
if (f.exists()) {
fileExists = true;
} else {
System.err.println(
"[jkf] Error configuring from file: " + logConfPath);
}
} else {
System.err.println(
"[jkf] Error configuring from file, file not specified !!");
}
// lets look if we got a config file for logging
if (fileExists) {
DOMConfigurator.configure(logConfPath);
} else {
System.out.println("[jkf] Using default Configuration ...");
// configure the jkfServerLogger
// Instantiate a file appender
jkfServerAppender =
new DailyRollingFileAppender();
// set the appenders properties
jkfServerAppender.setLayout(defaultLayout);
jkfServerAppender.setAppend(true);
jkfServerAppender.setDatePattern(LOGFILE_DATE_PATTERN);
jkfServerAppender.setFile(DEFAULT_LOG_FILENAME);
jkfServerAppender.setImmediateFlush(true);
// activate the appender
jkfServerAppender.activateOptions();
// default configuration of the logging system
jkfServerLogger.addAppender(jkfServerAppender);
jkfServerLogger.setLevel(jkfLogLevel);
jkfServerLogger.setAdditivity(false);
}
configured = true;
}
/**
* Sets the logConfPath.
* @param logConfPath The logConfPath to set
*/
public void setLogConfPath(String logConfPath) {
this.logConfPath = logConfPath;
}
public Level getServerLogLevel() {
return jkfServerLogger.getLevel();
}
public void setServerLogLevel(Level level) {
jkfServerLogger.setLevel(level);
if(jkfServerLogger.isDebugEnabled()) {
jkfServerLogger.debug("Setting new server log level to: " + level.toString());
}
}
/**
* Method for generating a Logger with the given loggerName. <br>
* The Loggers additivity flag is set to false and an RollingFileAppender <br>
* is added to the Logger. The file name attribute of the appender <br>
* is set to the loggerName.
*/
public Logger generateClientLogger(String loggerName) {
// configure the client appender
DailyRollingFileAppender clientAppender =
new DailyRollingFileAppender();
clientAppender.setLayout(defaultLayout);
clientAppender.setDatePattern(LOGFILE_DATE_PATTERN);
clientAppender.setAppend(false);
clientAppender.setImmediateFlush(true);
clientAppender.setFile(loggerName + ".log");
clientAppender.activateOptions();
Logger result = Logger.getLogger(loggerName);
result.setLevel(Level.DEBUG);
result.addAppender(clientAppender);
result.setAdditivity(false);
return result;
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -