logback 指定配置文件

原创
2014/01/26 14:51
阅读数 5.5K

原本想在karaf 中使用logback 形式的配置文件 结过发现Pax-logger-api X 全部覆盖了 然后就是使用karaf 自身的log4j吧

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");
    }

}

用途 

指定加载 环境变量 Karaf 下 etc/syslog.xml 作为默认的日志配置, 如果用户通过虚拟机-logback.configurationFile 指定了配置文件, 那么syslog.xml 将失效


展开阅读全文
打赏
0
0 收藏
分享
加载中
更多评论
打赏
0 评论
0 收藏
0
分享
返回顶部
顶部