logback 指定配置文件

本來想在karaf 中使用logback 形式的配置文件 結過發現Pax-logger-api X 所有覆蓋了 而後就是使用karaf 自身的log4j吧 java

public class SysLogInitActivator implements BundleActivator
{
    public static final String KARAF_HOME = "karaf_home";
    public static final String CONFIG_FILE = "syslog.xml";
    public static final String logbackPropName = "logback.configurationFile";

    public void start(BundleContext context) throws Exception
    {
        String logConfigPath = System.getProperty(logbackPropName);

        if (logConfigPath == null || "".equals(logConfigPath))
        {
            String _karaf_home = System.getenv(KARAF_HOME);
            
            if (_karaf_home == null || "".equals(_karaf_home))
            {
                throw new IllegalArgumentException(
                    "ERROR: Need Setting Environment Variables karaf_home");
            }else{
                System.out.println("INFO: system environment variables karaf_home is " + _karaf_home);
            }

            File logFile = new File(_karaf_home + File.separatorChar + "etc" + File.separatorChar
                + CONFIG_FILE);
            if (!logFile.exists())
            {
                System.err.println("WARN: not log config file " + logFile.getAbsolutePath()
                    + ", will use default.");
            }
            else
            {
                LoggerContext loggerContext = (LoggerContext) StaticLoggerBinder.getSingleton()
                    .getLoggerFactory();
                loggerContext.reset();
                JoranConfigurator configurator = new JoranConfigurator();
                configurator.setContext(loggerContext);
                configurator.doConfigure(logFile.getAbsoluteFile());
                System.out.println("INFO: use log config " + logFile.getAbsolutePath());
            }
        }
        else
        {
            System.out.println("INFO: use log config " + logConfigPath + " defined in JVM Param");
        }
    }

    public void stop(BundleContext context) throws Exception
    {
        System.out.println("sys log bundle close");
    }

}

用途  api

指定加載 環境變量 Karaf 下 etc/syslog.xml 做爲默認的日誌配置, 若是用戶經過虛擬機-logback.configurationFile 指定了配置文件, 那麼syslog.xml 將失效 spa

相關文章
相關標籤/搜索