public virtual void Init(IRuntimeServices rs) { // look for a level config property string level = (string)rs.GetProperty(RUNTIME_LOG_LEVEL_KEY); if (level != null) { // and set it accordingly EnabledLevel = toLevel(level); } // look for an errLevel config property string errLevel = (string)rs.GetProperty(RUNTIME_LOG_SYSTEM_ERR_LEVEL_KEY); if (errLevel != null) { SystemErrLevel = toLevel(errLevel); } }
/// <summary> Creates a new logging system or returns an existing one /// specified by the application. /// </summary> public static ILogSystem CreateLogSystem(IRuntimeServices runtimeServices) { ILogSystem logSystem; // if a logSystem was set as a configuration value, use that. // This is any class the user specifies. Object o = runtimeServices.GetProperty(RuntimeConstants.RUNTIME_LOG_LOGSYSTEM); logSystem = o as ILogSystem; if (logSystem != null) { logSystem.Init(runtimeServices); return(logSystem); } // otherwise, see if a class was specified. You // can put multiple classes, and we use the first one we find. // // Note that the default value of this property contains both the // AvalonLogSystem and the SimpleLog4JLogSystem for convenience - // so we use whichever we find. IList classes = new ArrayList(); Object obj = runtimeServices.GetProperty(RuntimeConstants.RUNTIME_LOG_LOGSYSTEM_CLASS); // we might have a list, or not - so check if (obj is IList) { classes = (IList)obj; } else if (obj is String) { classes.Add(obj); } // now run through the list, trying each. It's ok to // fail with a class not found, as we do this to also // search out a default simple file logger foreach (String className in classes) { if (className != null && className.Length > 0) { runtimeServices.Info(string.Format("Trying to use logger class {0}", className)); try { Type type = Type.GetType(className); o = Activator.CreateInstance(type); logSystem = o as ILogSystem; if (logSystem == null) { runtimeServices.Error(string.Format("The specified logger class {0} isn't a valid LogSystem", className)); } else { logSystem.Init(runtimeServices); runtimeServices.Info(string.Format("Using logger class {0}", className)); return(logSystem); } } catch (ApplicationException applicationException) { runtimeServices.Debug( string.Format("Couldn't find class {0} or necessary supporting classes in classpath. Exception : {1}", className, applicationException)); } } } // if the above failed, then we are in deep doo-doo, as the // above means that either the user specified a logging class // that we can't find, there weren't the necessary // dependencies in the classpath for it, or there were no // dependencies for the default logger. // Since we really don't know, // then take a wack at the log4net as a last resort. try { logSystem = new NullLogSystem(); logSystem.Init(runtimeServices); } catch (ApplicationException applicationException) { String error = string.Format( "PANIC : NVelocity cannot find any of the specified or default logging systems in the classpath, or the classpath doesn't contain the necessary classes to support them. Please consult the documentation regarding logging. Exception : {0}", applicationException); Console.Out.WriteLine(error); Console.Error.WriteLine(error); throw; } runtimeServices.Info("Using log4net as logger of final resort."); return(logSystem); }
/// <summary> Creates a new logging system or returns an existing one /// specified by the application. /// </summary> public static ILogSystem CreateLogSystem(IRuntimeServices runtimeServices) { ILogSystem logSystem; // if a logSystem was set as a configuration value, use that. // This is any class the user specifies. Object o = runtimeServices.GetProperty(RuntimeConstants.RUNTIME_LOG_LOGSYSTEM); logSystem = o as ILogSystem; if (logSystem != null) { logSystem.Init(runtimeServices); return logSystem; } // otherwise, see if a class was specified. You // can put multiple classes, and we use the first one we find. // // Note that the default value of this property contains both the // AvalonLogSystem and the SimpleLog4JLogSystem for convenience - // so we use whichever we find. IList classes = new ArrayList(); Object obj = runtimeServices.GetProperty(RuntimeConstants.RUNTIME_LOG_LOGSYSTEM_CLASS); // we might have a list, or not - so check if (obj is IList) { classes = (IList) obj; } else if (obj is String) { classes.Add(obj); } // now run through the list, trying each. It's ok to // fail with a class not found, as we do this to also // search out a default simple file logger foreach(String className in classes) { if (className != null && className.Length > 0) { runtimeServices.Info(string.Format("Trying to use logger class {0}", className)); try { Type type = Type.GetType(className); o = Activator.CreateInstance(type); logSystem = o as ILogSystem; if (logSystem == null) { runtimeServices.Error(string.Format("The specified logger class {0} isn't a valid LogSystem", className)); } else { logSystem.Init(runtimeServices); runtimeServices.Info(string.Format("Using logger class {0}", className)); return logSystem; } } catch(ApplicationException applicationException) { runtimeServices.Debug( string.Format("Couldn't find class {0} or necessary supporting classes in classpath. Exception : {1}", className, applicationException)); } } } // if the above failed, then we are in deep doo-doo, as the // above means that either the user specified a logging class // that we can't find, there weren't the necessary // dependencies in the classpath for it, or there were no // dependencies for the default logger. // Since we really don't know, // then take a wack at the log4net as a last resort. try { logSystem = new NullLogSystem(); logSystem.Init(runtimeServices); } catch(ApplicationException applicationException) { String error = string.Format( "PANIC : NVelocity cannot find any of the specified or default logging systems in the classpath, or the classpath doesn't contain the necessary classes to support them. Please consult the documentation regarding logging. Exception : {0}", applicationException); Console.Out.WriteLine(error); Console.Error.WriteLine(error); throw; } runtimeServices.Info("Using log4net as logger of final resort."); return logSystem; }
// Creates a new logging system or returns an existing one // specified by the application. private static ILogChute CreateLogChute(IRuntimeServices rsvc) { Log log = rsvc.Log; /* If a ILogChute or LogSystem instance was set as a configuration * value, use that. This is any class the user specifies. */ System.Object o = rsvc.GetProperty(RuntimeConstants.RUNTIME_LOG_LOGSYSTEM); if (o != null) { // first check for a ILogChute if (o is ILogChute) { try { ((ILogChute)o).Init(rsvc); return((ILogChute)o); } catch (System.Exception e) { System.String msg = "Could not init runtime.log.logsystem " + o; log.Error(msg, e); throw new VelocityException(msg, e); } } else { System.String msg = o.GetType().FullName + " object set as runtime.log.logsystem is not a valid log implementation."; log.Error(msg); throw new VelocityException(msg); } } /* otherwise, see if a class was specified. You can Put multiple * classes, and we use the first one we find. * * Note that the default value of this property contains the * AvalonLogChute, the Log4JLogChute, CommonsLogLogChute, * ServletLogChute, and the JdkLogChute for * convenience - so we use whichever we works first. */ System.Collections.IList classes = new System.Collections.ArrayList(); System.Object obj = rsvc.GetProperty(RuntimeConstants.RUNTIME_LOG_LOGSYSTEM_CLASS); /* * we might have a list, or not - so check */ if (obj is System.Collections.IList) { classes = (System.Collections.IList)obj; } else if (obj is System.String) { classes.Add(obj); } /* * now run through the list, trying each. It's ok to * fail with a class not found, as we do this to also * search out a default simple file logger */ for (System.Collections.IEnumerator ii = classes.GetEnumerator(); ii.MoveNext();) { System.String claz = (System.String)ii.Current; if (claz != null && claz.Length > 0) { log.Debug("Trying to use logger class " + claz); try { o = Activator.CreateInstance(Type.GetType(claz.Replace(';', ','))); if (o is ILogChute) { ((ILogChute)o).Init(rsvc); log.Debug("Using logger class " + claz); return((ILogChute)o); } else { System.String msg = "The specified logger class " + claz + " does not implement the " + typeof(ILogChute).FullName + " interface."; log.Error(msg); // be extra informative if it appears to be a classloader issue // this should match all our provided LogChutes if (IsProbablyProvidedLogChute(claz)) { // if it's likely to be ours, tip them off about classloader stuff log.Error("This appears to be a ClassLoader issue. Check for multiple Velocity jars in your classpath."); } throw new VelocityException(msg); } } catch (System.ApplicationException ncdfe) { // note these errors for anyone debugging the app if (IsProbablyProvidedLogChute(claz)) { log.Debug("Target log system for " + claz + " is not available (" + ncdfe.ToString() + "). Falling back to next log system..."); } else { log.Debug("Couldn't find class " + claz + " or necessary supporting classes in classpath.", ncdfe); } } catch (System.Exception e) { System.String msg = "Failed to initialize an instance of " + claz + " with the current runtime configuration."; // Log unexpected Init exception at higher priority log.Error(msg, e); throw new VelocityException(msg, e); } } } /* If the above failed, that means either the user specified a * logging class that we can't find, there weren't the necessary * dependencies in the classpath for it, or there were the same * problems for the default loggers, log4j and Java1.4+. * Since we really don't know and we want to be sure the user knows * that something went wrong with the logging, let's fall back to the * surefire SystemLogChute. No panicking or failing to Log!! */ ILogChute slc = new NullLogChute(); slc.Init(rsvc); log.Debug("Using SystemLogChute."); return(slc); }