]>
git.argeo.org Git - gpl/argeo-slc.git/blob - runtime/org.argeo.slc.support.simple/src/main/java/org/argeo/slc/logging/Log4jUtils.java
1 package org
.argeo
.slc
.logging
;
3 import java
.io
.IOException
;
4 import java
.io
.InputStream
;
5 import java
.util
.Properties
;
7 import org
.apache
.commons
.io
.IOUtils
;
8 import org
.apache
.log4j
.LogManager
;
9 import org
.apache
.log4j
.PropertyConfigurator
;
10 import org
.argeo
.slc
.core
.SlcException
;
11 import org
.springframework
.core
.io
.DefaultResourceLoader
;
12 import org
.springframework
.util
.ResourceUtils
;
13 import org
.springframework
.util
.SystemPropertyUtils
;
15 public class Log4jUtils
{
18 * Configure log4j based on properties, with the following priorities (from
19 * highest to lowest):<br>
20 * 1. System properties<br>
21 * 2. configuration file itself
23 public static void initLog4j(String configuration
) {
24 // clears previous configuration
27 ClassLoader cl
= Log4jUtils
.class.getClassLoader();
28 Properties properties
= new Properties();
29 if (configuration
!= null) {
30 InputStream in
= null;
33 .startsWith(ResourceUtils
.CLASSPATH_URL_PREFIX
)) {
34 String path
= configuration
35 .substring(ResourceUtils
.CLASSPATH_URL_PREFIX
37 in
= cl
.getResourceAsStream(path
);
39 in
= new DefaultResourceLoader(cl
).getResource(
40 configuration
).getInputStream();
44 } catch (IOException e
) {
45 throw new SlcException("Cannot load properties from "
48 IOUtils
.closeQuietly(in
);
52 // Overrides with System properties
53 overrideLog4jProperties(properties
, System
.getProperties());
55 PropertyConfigurator
.configure(properties
);
58 private static void overrideLog4jProperties(Properties target
,
59 Properties additional
) {
60 for (Object obj
: additional
.keySet()) {
61 String key
= obj
.toString();
62 if (key
.startsWith("log4j.")) {
63 if (!key
.equals("log4j.configuration")) {
64 String value
= SystemPropertyUtils
65 .resolvePlaceholders(additional
.getProperty(key
));
66 target
.put(key
, value
);
72 public static void shutDownLog4j() {
73 LogManager
.shutdown();
76 private Log4jUtils() {