Logger used in case of failure. Does nothing. *
Inheritance: LogSystem
Example #1
0
		/// <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;
		}
Example #2
0
        /// <summary>  Creates a new logging system or returns an existing one
        /// specified by the application.
        /// </summary>
        public static LogSystem createLogSystem(RuntimeServices rsvc)
        {
            // if a logSystem was set as a configuation value, use that.
            // This is any class the user specifies.
            Object o = rsvc.getProperty(RuntimeConstants_Fields.RUNTIME_LOG_LOGSYSTEM);

            if (o != null && o is LogSystem)
            {
                ((LogSystem)o).Init(rsvc);

                return((LogSystem)o);
            }

            // 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     = rsvc.getProperty(RuntimeConstants_Fields.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 clazz in classes)
            {
                if (clazz != null && clazz.Length > 0)
                {
                    rsvc.info("Trying to use logger class " + clazz);

                    try
                    {
                        Type type = Type.GetType(clazz);
                        o = Activator.CreateInstance(type);

                        if (o is LogSystem)
                        {
                            ((LogSystem)o).Init(rsvc);

                            rsvc.info("Using logger class " + clazz);

                            return((LogSystem)o);
                        }
                        else
                        {
                            rsvc.error("The specifid logger class " + clazz + " isn't a valid LogSystem");
                        }
                    }
                    catch (ApplicationException ncdfe)
                    {
                        rsvc.debug("Couldn't find class " + clazz + " or necessary supporting classes in classpath. Exception : " + ncdfe);
                    }
                }
            }

            // 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.
            LogSystem als = null;

            try
            {
                als = new NullLogSystem();
                als.Init(rsvc);
            }
            catch (ApplicationException ncdfe)
            {
                String errstr = "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 : " + ncdfe;

                Console.Out.WriteLine(errstr);
                Console.Error.WriteLine(errstr);

                throw;
            }

            rsvc.info("Using log4net as logger of final resort.");

            return(als);
        }
Example #3
0
        /// <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);
        }