1 6 7 package org.apache.catalina.logger; 9 10 import java.io.File ; 11 import java.io.FileInputStream ; 12 import java.io.FileWriter ; 13 import java.io.IOException ; 14 import java.io.PrintWriter ; 15 import java.sql.Timestamp ; 16 import java.util.Enumeration ; 17 import java.util.Iterator ; 18 import java.util.Locale ; 19 import java.util.Properties ; 20 import java.util.ResourceBundle ; 21 import java.util.Set ; 22 23 import javax.management.MBeanServerConnection ; 24 import javax.management.ObjectName ; 25 import javax.management.remote.JMXConnector ; 26 import javax.management.remote.JMXConnectorFactory ; 27 import javax.management.remote.JMXServiceURL ; 28 29 import org.apache.catalina.LifecycleException; 30 import org.apache.catalina.util.StringManager; 31 import org.objectweb.util.monolog.Monolog; 32 import org.objectweb.util.monolog.api.BasicLevel; 33 import org.objectweb.util.monolog.api.Level; 34 import org.objectweb.util.monolog.api.LoggerFactory; 35 import org.objectweb.util.monolog.wrapper.common.Configurable; 36 37 import com.lutris.util.Config; 38 import com.lutris.util.ConfigException; 39 40 41 49 50 public class TomcatMonologFileLogger 51 extends LoggerBase { 52 private String _options; 53 private transient boolean _started; 54 private org.objectweb.util.monolog.api.LoggerFactory lf; 55 private org.objectweb.util.monolog.api.Logger logger = null; 56 private String path, jonas, fileName; 57 private static ResourceBundle rb = null; 58 public static String PROPERTY_FILE; 59 static ObjectName objectName; 60 62 public TomcatMonologFileLogger () throws ConfigException { 63 64 try { 65 configure(); 66 this.logger = lf.getLogger("TomcatMonologFileLogger"); 67 68 }catch (ConfigException ex) { 69 throw new ConfigException("TomcatMonologFileLogger:Cannot configure logger. Logger not initialized."); 70 } 71 72 } 73 74 private String date = ""; 75 76 79 private String directory = "logs"; 80 81 82 85 protected static final String info = 86 "org.apache.catalina.logger.FileLogger/1.0"; 87 88 89 92 private String prefix = "catalina."; 93 94 95 98 private StringManager sm = 99 StringManager.getManager(Constants.Package); 100 101 102 105 private boolean started = false; 106 107 108 111 private String suffix = ".log"; 112 113 114 117 private boolean timestamp = false; 118 119 120 123 private PrintWriter writer = null; 124 125 126 128 129 137 public void log(String msg){ 138 139 141 Timestamp ts = new Timestamp (System.currentTimeMillis()); 142 String tsString = ts.toString().substring(0, 19); 143 String tsDate = tsString.substring(0, 10); 144 String logString = ""; 145 146 156 157 159 if (timestamp) { 160 logString = tsString + " " + msg; 161 } else { 162 logString = msg; 163 } 164 165 166 Level priority=BasicLevel.LEVEL_INFO; 167 168 174 175 178 logger.log(priority, 179 "", 180 ""+msg, 181 null); 182 183 } 184 185 public synchronized void configure(String monologConfFile) 186 throws ConfigException { 187 188 if (monologConfFile==null || monologConfFile.length() == 0) { 189 throw new ConfigException( 190 "impossible to configure monolog without configuration file"); 191 } 192 String b = null; 194 Properties p = new Properties (); 195 try { 196 197 p.load(new FileInputStream (monologConfFile)); 198 b = p.getProperty("log.config.classname", null); 200 201 if (b == null) { 202 throw new ConfigException("Malformed configuration log file:" 203 + " log.config.classname not available"); 204 } 205 206 lf = (LoggerFactory) Class.forName(b).newInstance(); 208 p.put(Configurable.LOG_CONFIGURATION_TYPE, Configurable.PROPERTY); 210 p.put(Configurable.LOG_CONFIGURATION_FILE, monologConfFile); 211 213 ((Configurable) lf).configure(p); 214 215 216 } 218 catch (Exception e) { 219 throw new ConfigException("Malformed configuration log file:" 220 + " log.config.classname not available"); 221 } 222 } 223 224 public synchronized void configure() 225 throws ConfigException { 226 String b = null; 227 String propkey; 228 String propvalue; 229 Properties props = new Properties (); 230 231 233 234 try{ 235 findObjectName(); 236 if (objectName != null) 237 PROPERTY_FILE = objectName.getKeyProperty("fname"); 238 rb = ResourceBundle.getBundle(PROPERTY_FILE,new Locale ("en","US"), ClassLoader.getSystemClassLoader()); 239 240 Enumeration enumeration = rb.getKeys(); 241 242 243 while (enumeration.hasMoreElements()) 244 { 245 246 propkey = (String ) enumeration.nextElement(); 247 propvalue = rb.getString(propkey); 248 props.setProperty(propkey, propvalue); 249 } 250 251 252 } catch (Exception e){ 253 throw new ConfigException("Logger configuration file could not be found: logger could not be initialized!"); 254 } 255 256 try { 257 258 b = props.getProperty("log.config.classname", null); 260 if (b == null) { 261 throw new ConfigException("Malformed configuration log file:" 262 + " log.config.classname not available"); 263 } 264 265 267 props.put(Configurable.LOG_CONFIGURATION_TYPE, Configurable.PROPERTY); 268 lf = Monolog.getMonologFactory(b); 269 270 271 } 273 catch (Exception e) { 274 throw new ConfigException("Malformed configuration log file:" 275 + " log.config.classname not available"); 276 } 277 278 } 279 280 281 286 public void configure(Config logConfig) throws ConfigException { 287 String fileName = null; 288 289 if (logConfig.containsKey("Monolog")) { 290 fileName = logConfig.getString("Monolog"); 291 try{ 292 configure(fileName); 293 } catch (Exception ex) { 294 try { 295 configure(); 296 }catch (ConfigException cex) { 297 throw new ConfigException("Cannot configure logger. Config file is null."); 298 } 299 } 300 301 } else { 302 try { 303 configure(); 304 }catch (ConfigException ex) { 305 throw new ConfigException("Cannot configure logger. Logger not initialized."); 306 } 307 } 308 } 309 310 311 314 private void findObjectName () throws Exception { 315 JMXConnector connector = null; 316 try { 317 318 String serviceName = "jonas"; 319 320 serviceName = System.getProperty("jonas.name"); 322 if ( serviceName == null) { 323 serviceName = "jonas"; 324 } 325 326 327 328 JMXServiceURL url = new JMXServiceURL ("service:jmx:rmi://localhost/jndi/jrmpconnector_" + serviceName); 330 331 connector = JMXConnectorFactory.connect(url); 333 334 MBeanServerConnection connection = connector.getMBeanServerConnection(); 337 338 objectName = new ObjectName (serviceName + ":type=service,name=log,*"); 339 340 Set mBeans = connection.queryNames(objectName, null); 341 int l = mBeans.size(); 342 if ((l) > 1) { 343 throw new Exception ("MBean set size not equal 1: " + l); 344 }else if (l == 0){ 345 objectName = null; 346 if (connector!=null){ 347 try { 348 connector.close(); 349 } catch (Exception exept){} 350 } 351 return; 352 } 353 Iterator i = mBeans.iterator(); 354 objectName = (ObjectName )i.next(); 355 } catch (Exception e) { 356 throw new Exception (e); 357 }finally { 358 if (connector!=null){ 359 try { 360 connector.close(); 361 } catch (Exception exept){} 362 } 363 } 364 return; 365 } 366 367 private void close() { 368 369 if (writer == null) 370 return; 371 writer.flush(); 372 writer.close(); 373 writer = null; 374 date = ""; 375 376 } 377 378 381 private void open() { 382 383 File dir = new File (directory); 385 if (!dir.isAbsolute()) 386 dir = new File (System.getProperty("catalina.base"), directory); 387 dir.mkdirs(); 388 389 try { 391 String pathname = dir.getAbsolutePath() + File.separator + 392 prefix + date + suffix; 393 writer = new PrintWriter (new FileWriter (pathname, true), true); 394 } catch (IOException e) { 395 writer = null; 396 } 397 398 } 399 400 public void start() throws LifecycleException {} 401 402 403 404 412 public void stop() throws LifecycleException {} 413 414 } 415 | Popular Tags |