1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34 package fr.paris.lutece.portal.service.util;
35
36 import org.apache.log4j.BasicConfigurator;
37 import org.apache.log4j.Logger;
38 import org.apache.log4j.PropertyConfigurator;
39
40 import java.io.File;
41 import java.io.InputStream;
42
43 import java.util.Properties;
44
45
46
47
48
49 public final class AppLogService
50 {
51
52 private static final String LOGGER_EVENTS = "lutece.event";
53 private static final String LOGGER_DEBUG = "lutece.debug";
54 private static final String LOGGER_ERRORS = "lutece.error";
55 private static final String SYSTEM_PROPERTY_LOG4J_CONFIGURATION = "log4j.configuration";
56
57
58 private static final String ALTERNATE_LOG_OVERRIDE_PATH = "override";
59 private static final String ALTERNATE_LOG_FILE = "log.properties";
60 private static Logger _loggerEvents = Logger.getLogger( LOGGER_EVENTS );
61 private static Logger _loggerErrors = Logger.getLogger( LOGGER_ERRORS );
62 private static Logger _loggerDebug = Logger.getLogger( LOGGER_DEBUG );
63
64
65
66
67 private AppLogService( )
68 {
69 }
70
71
72
73
74 public static void preinit( )
75 {
76 BasicConfigurator.configure();
77 info("Lutece logs pre-initialized: sending all logs to stdout.");
78 }
79
80
81
82
83
84
85 public static void init( String strConfigPath, String strConfigFile )
86 {
87 BasicConfigurator.resetConfiguration();
88
89 try
90 {
91 _loggerEvents.setAdditivity( false );
92
93 _loggerDebug.setAdditivity( false );
94
95 String strAbsoluteConfigDirectoryPath = AppPathService.getAbsolutePathFromRelativePath( strConfigPath );
96
97 String strAlternateFilePath = strAbsoluteConfigDirectoryPath +
98 ( strAbsoluteConfigDirectoryPath.endsWith( "/" ) ? "" : "/" ) + ALTERNATE_LOG_OVERRIDE_PATH;
99
100 File alternateLogFile = new File( strAlternateFilePath + File.separator + ALTERNATE_LOG_FILE );
101
102 boolean bAlternateConfigFile = alternateLogFile.exists( );
103
104 InputStream is;
105 String strLog4jConfigFile;
106
107 if ( bAlternateConfigFile )
108 {
109
110 is = AppPathService.getResourceAsStream( strConfigPath + ( strConfigPath.endsWith( "/" ) ? "" : "/" ) +
111 ALTERNATE_LOG_OVERRIDE_PATH + "/", ALTERNATE_LOG_FILE );
112 strLog4jConfigFile = alternateLogFile.getAbsolutePath( );
113 }
114 else
115 {
116
117 is = AppPathService.getResourceAsStream( strConfigPath, strConfigFile );
118 strLog4jConfigFile = strAbsoluteConfigDirectoryPath +
119 ( ( strAbsoluteConfigDirectoryPath.endsWith( "/" ) ) ? "" : "/" ) + strConfigFile;
120 }
121
122 Properties props = new Properties( );
123 props.load( is );
124 PropertyConfigurator.configure( props );
125 is.close( );
126
127
128
129 System.setProperty( SYSTEM_PROPERTY_LOG4J_CONFIGURATION, strLog4jConfigFile );
130
131 if ( bAlternateConfigFile )
132 {
133 debug( "Loaded log properties from alternate log.properties file " );
134 }
135 }
136 catch ( Exception e )
137 {
138 System.err.println( "Bad Configuration of Log4j : " + e );
139 }
140 info("Lutece logs initialized, using configured property files to define levels and appenders.");
141 }
142
143
144
145
146
147
148
149
150
151 public static boolean isDebugEnabled( )
152 {
153 return _loggerDebug.isDebugEnabled( );
154 }
155
156
157
158
159
160
161 public static void debug( Object objToLog )
162 {
163 if ( _loggerDebug.isDebugEnabled( ) )
164 {
165 _loggerDebug.debug( objToLog );
166 }
167 }
168
169
170
171
172
173
174
175 public static boolean isDebugEnabled( String strLogger )
176 {
177 Logger logger = Logger.getLogger( strLogger );
178
179 return logger.isDebugEnabled( );
180 }
181
182
183
184
185
186
187
188 public static void debug( String strLogger, Object objToLog )
189 {
190 Logger logger = Logger.getLogger( strLogger );
191
192 if ( logger.isDebugEnabled( ) )
193 {
194 logger.debug( objToLog );
195 }
196 }
197
198
199
200
201
202
203 public static void error( Object objToLog )
204 {
205 if ( _loggerErrors != null )
206 {
207 _loggerErrors.error( objToLog );
208 }
209 }
210
211
212
213
214
215
216
217
218
219 public static void error( Object message, Throwable t )
220 {
221 if ( _loggerErrors != null )
222 {
223 _loggerErrors.error( message, t );
224 }
225 }
226
227
228
229
230
231
232 public static void info( Object objToLog )
233 {
234 if ( ( _loggerEvents != null ) && _loggerEvents.isInfoEnabled( ) )
235 {
236 _loggerEvents.info( objToLog );
237 }
238 }
239 }