下面列出了org.apache.log4j.xml.DOMConfigurator#configure ( ) 实例代码,或者点击链接到github查看源代码,也可以在右侧发表评论。
protected static void configureLogging(Properties properties) {
// Init logging: Try standard log4j configuration mechanism before falling back to
// provided logging configuration
String log4jConfig = System.getProperty("log4j.configuration");
if (null == log4jConfig) {
if (properties.containsKey(PropertiesBasedServerSetupBuilder.GREENMAIL_VERBOSE)) {
DOMConfigurator.configure(GreenMailStandaloneRunner.class.getResource("/log4j-verbose.xml"));
} else {
DOMConfigurator.configure(GreenMailStandaloneRunner.class.getResource("/log4j.xml"));
}
} else {
if (log4jConfig.toLowerCase().endsWith(".xml")) {
DOMConfigurator.configure(log4jConfig);
} else {
PropertyConfigurator.configure(log4jConfig);
}
}
}
private void reloadConfig() throws MalformedURLException, FactoryConfigurationError, BException {
String blurHome = System.getenv("BLUR_HOME");
if (blurHome != null) {
File blurHomeFile = new File(blurHome);
if (blurHomeFile.exists()) {
File log4jFile = new File(new File(blurHomeFile, "conf"), "log4j.xml");
if (log4jFile.exists()) {
LOG.info("Reseting log4j config from [{0}]", log4jFile);
LogManager.resetConfiguration();
DOMConfigurator.configure(log4jFile.toURI().toURL());
return;
}
}
}
URL url = TableAdmin.class.getResource("/log4j.xml");
if (url != null) {
LOG.info("Reseting log4j config from classpath resource [{0}]", url);
LogManager.resetConfiguration();
DOMConfigurator.configure(url);
return;
}
throw new BException("Could not locate log4j file to reload, doing nothing.");
}
void common(int number) throws Exception {
DOMConfigurator.configure("input/xml/customLogger"+number+".xml");
int i = -1;
Logger root = Logger.getRootLogger();
logger.trace("Message " + ++i);
logger.debug("Message " + ++i);
logger.warn ("Message " + ++i);
logger.error("Message " + ++i);
logger.fatal("Message " + ++i);
Exception e = new Exception("Just testing");
logger.debug("Message " + ++i, e);
Transformer.transform(
"output/temp", FILTERED,
new Filter[] {
new LineNumberFilter(), new SunReflectFilter(),
new JunitTestRunnerFilter()
});
assertTrue(Compare.compare(FILTERED, "witness/customLogger."+number));
}
@BeforeClass
public static void testSetup() throws IOException, SQLException, DAOConfigurationException {
URL configFileResource = PostgresqlDAOTest.class.getResource("/com/ast/processserver/resources/log4j.xml");
DOMConfigurator.configure(configFileResource);
Config.loadProperties();
String dataSource = "postgresql";
DAOFactory factory = new DAOFactory(dataSource);
connection = ConnectionPoolFactory.getConnectionPool(dataSource).getConnection();
workspaceDAO = factory.getWorkspaceDao(connection);
userDao = factory.getUserDao(connection);
objectDao = factory.getItemDAO(connection);
oversionDao = factory.getItemVersionDAO(connection);
}
public static void main(final String[] args) {
final String log4jConfiguration = System.getProperties().getProperty("log4j.configuration");
if (StringUtils.isNotBlank(log4jConfiguration)) {
final String parsedConfiguration = PathUtils.clean(StringUtils.removeStart(log4jConfiguration, "file:"));
final File configFile = new File(parsedConfiguration);
if (configFile.exists()) {
DOMConfigurator.configure(parsedConfiguration);
} else {
BasicConfigurator.configure();
}
}
log.info("Starting Merge Tool");
Thread.setDefaultUncaughtExceptionHandler(new Thread.UncaughtExceptionHandler() {
@Override
public void uncaughtException(final Thread thread, final Throwable throwable) {
log.error("Uncaught exception in " + thread.getName(), throwable);
}
});
final int returnCode = setupAndRun(args);
log.info("Finished running Merge Tool");
System.exit(returnCode);
}
public static void initLog4J(PropertyHandler propertyHandler) {
LOG.info("**************** Init LOG4J");
if (propertyHandler != null) {
Path path = Paths.get(propertyHandler.getProperty("LOG4J", "log4j.xml"));
if (Files.exists(path)) {
LogManager.resetConfiguration();
DOMConfigurator.configure(path.toAbsolutePath().toString());
LOG.info("Loading log4j config from " + path.toAbsolutePath().toString());
}
}
}
public static void initLog4J(PropertyHandler propertyHandler) {
LOG.info("**************** Init LOG4J");
if (propertyHandler != null) {
Path path = Paths.get(propertyHandler.getProperty("LOG4J", "log4j.xml"));
if (Files.exists(path)) {
LogManager.resetConfiguration();
DOMConfigurator.configure(path.toAbsolutePath().toString());
LOG.info("Loading log4j config from " + path.toAbsolutePath().toString());
}
}
}
@Before
public void IntiRes() {
DOMConfigurator.configure("res/appConfig/log4j.xml");
System.setProperty("java.net.preferIPv4Stack", "true"); //Disable IPv6 in JVM
springContext = new FileSystemXmlApplicationContext("res/springConfig/spring-context.xml");
/**初始化spring容器*/
testPubSub = (TestPubSub) springContext.getBean("testPubSub");
}
static void init(String portStr, String configFile) {
try {
port = Integer.parseInt(portStr);
} catch(java.lang.NumberFormatException e) {
e.printStackTrace();
usage("Could not interpret port number ["+ portStr +"].");
}
if(configFile.endsWith(".xml")) {
DOMConfigurator.configure(configFile);
} else {
PropertyConfigurator.configure(configFile);
}
}
/**
* Initialize log4j from the given file location, with no config file refreshing.
* Assumes an XML file in case of a ".xml" file extension, and a properties file
* otherwise.
* @param location the location of the config file: either a "classpath:" location
* (e.g. "classpath:myLog4j.properties"), an absolute file URL
* (e.g. "file:C:/log4j.properties), or a plain absolute path in the file system
* (e.g. "C:/log4j.properties")
* @throws FileNotFoundException if the location specifies an invalid file path
*/
public static void initLogging(String location) throws FileNotFoundException {
String resolvedLocation = SystemPropertyUtils.resolvePlaceholders(location);
URL url = ResourceUtils.getURL(resolvedLocation);
if (ResourceUtils.URL_PROTOCOL_FILE.equals(url.getProtocol()) && !ResourceUtils.getFile(url).exists()) {
throw new FileNotFoundException("Log4j config file [" + resolvedLocation + "] not found");
}
if (resolvedLocation.toLowerCase().endsWith(XML_FILE_EXTENSION)) {
DOMConfigurator.configure(url);
}
else {
PropertyConfigurator.configure(url);
}
}
static public void main(String[] args) throws Exception {
if(args.length != 5) {
usage("Wrong number of arguments.");
}
String tcfBindingName = args[0];
String topicBindingName = args[1];
String username = args[2];
String password = args[3];
String configFile = args[4];
if(configFile.endsWith(".xml")) {
DOMConfigurator.configure(configFile);
} else {
PropertyConfigurator.configure(configFile);
}
new JMSSink(tcfBindingName, topicBindingName, username, password);
BufferedReader stdin = new BufferedReader(new InputStreamReader(System.in));
// Loop until the word "exit" is typed
System.out.println("Type \"exit\" to quit JMSSink.");
while(true){
String s = stdin.readLine( );
if (s.equalsIgnoreCase("exit")) {
System.out.println("Exiting. Kill the application if it does not exit "
+ "due to daemon threads.");
return;
}
}
}
/**
* The main method.
* @param args the command arguments
*/
public static void main(final String[] args) {
DOMConfigurator.configure("./log4j.xml");
final String serialPortName;
final int channel;
final int pan;
final boolean resetNetwork;
try {
serialPortName = args[0];
channel = Integer.parseInt(args[1]);
pan = parseDecimalOrHexInt(args[2]);
resetNetwork = args[3].equals("true");
} catch (final Throwable t) {
t.printStackTrace();
System.out.println(USAGE);
return;
}
final SerialPort serialPort = new SerialPortImpl(serialPortName, DEFAULT_BAUD_RATE);
final ZigBeeConsole console = new ZigBeeConsole(serialPort,pan,channel,resetNetwork);
console.start();
}
/**
When called wihtout arguments, this program will just print
<pre>
DEBUG [main] some.cat - Hello world.
</pre>
and exit.
<b>However, it can be called with a configuration file in XML or
properties format.
*/
static public void main(String[] args) {
if(args.length == 0) {
// Note that the appender is added to root but that the log
// request is made to an instance of MyLogger. The output still
// goes to System.out.
Logger root = Logger.getRootLogger();
Layout layout = new PatternLayout("%p [%t] %c (%F:%L) - %m%n");
root.addAppender(new ConsoleAppender(layout, ConsoleAppender.SYSTEM_OUT));
}
else if(args.length == 1) {
if(args[0].endsWith("xml")) {
DOMConfigurator.configure(args[0]);
} else {
PropertyConfigurator.configure(args[0]);
}
} else {
usage("Incorrect number of parameters.");
}
try {
MyLogger c = (MyLogger) MyLogger.getLogger("some.cat");
c.trace("Hello");
c.debug("Hello");
} catch(ClassCastException e) {
LogLog.error("Did you forget to set the factory in the config file?", e);
}
}
@Override
public void init() {
DOMConfigurator.configure("src/test/resources/log4j-example.xml");
}
@Override
public void init() {
DOMConfigurator.configure("src/test/resources/log4j-example.xml");
}
@Override
public void init() {
DOMConfigurator.configure("src/test/resources/log4j-example.xml");
}
@Override
public void contextInitialized(ServletContextEvent servletContextEvent) {
ToStringBuilder.setDefaultStyle(ToStringStyle.SHORT_PREFIX_STYLE);
DOMConfigurator.configure("../data/conf/log4j.xml");
}
public static void loadProperties(String configFileUrl) throws IOException {
logger.info(String.format("Loading config file: '%s'", configFileUrl));
URL log4jResource = Config.class.getResource("/log4j.xml");
DOMConfigurator.configure(log4jResource);
properties = new Properties();
properties.load(new FileInputStream(configFileUrl));
validateProperties();
logger.info("Configuration file loaded");
displayConfiguration();
}
/**
* Try to init stuff.
*/
public static void init() {
String log4jPath = Play.configuration.getProperty("application.log.path", "/log4j.xml");
URL log4jConf = Logger.class.getResource(log4jPath);
boolean isXMLConfig = log4jPath.endsWith(".xml");
if (log4jConf == null) { // try again with the .properties
isXMLConfig = false;
log4jPath = Play.configuration.getProperty("application.log.path", "/log4j.properties");
log4jConf = Logger.class.getResource(log4jPath);
}
if (log4jConf == null) {
Properties shutUp = new Properties();
shutUp.setProperty("log4j.rootLogger", "OFF");
PropertyConfigurator.configure(shutUp);
} else if (Logger.log4j == null) {
if (log4jConf.getFile().indexOf(Play.applicationPath.getAbsolutePath()) == 0) {
// The log4j configuration file is located somewhere in the application folder,
// so it's probably a custom configuration file
configuredManually = true;
}
if (isXMLConfig) {
DOMConfigurator.configure(log4jConf);
} else {
PropertyConfigurator.configure(log4jConf);
}
Logger.log4j = org.apache.log4j.Logger.getLogger("play");
// In test mode, append logs to test-result/application.log
if (Play.runingInTestMode()) {
org.apache.log4j.Logger rootLogger = org.apache.log4j.Logger.getRootLogger();
try {
if (!Play.getFile("test-result").exists()) {
Play.getFile("test-result").mkdir();
}
Appender testLog = new FileAppender(new PatternLayout("%d{DATE} %-5p ~ %m%n"), Play.getFile("test-result/application.log").getAbsolutePath(), false);
rootLogger.addAppender(testLog);
} catch (Exception e) {
e.printStackTrace();
}
}
}
}
@Before
public void IntiRes() {
DOMConfigurator.configure("res/appConfig/log4j.xml");
System.setProperty("java.net.preferIPv4Stack", "true"); //Disable IPv6 in JVM
springContext = new FileSystemXmlApplicationContext("res/springConfig/spring-context.xml");
List<String> list = Arrays.asList("");
List<String> stringList = new ArrayList<String>();
// JRedisSerializationUtils.fastDeserialize(JRedisSerializationUtils.fastSerialize(list));
//
// JRedisSerializationUtils.kryoDeserialize(JRedisSerializationUtils.kryoSerialize(list));
System.out.println
((list instanceof ArrayList) ? "list is ArrayList"
: "list not ArrayList?");
System.out.println
((stringList instanceof ArrayList) ? "list is ArrayList"
: "list not ArrayList?");
KryoThreadLocalSer.getInstance().ObjDeserialize( KryoThreadLocalSer.getInstance().ObjSerialize(list));
}