IBM内容导航器插件:使用log4j记录

发布于 2025-01-27 07:39:48 字数 722 浏览 4 评论 0原文

嗨,所有IBM ICN开发人员,

我正在努力编写ICN插件。我希望我的Logger语句登录到单独的文件中,而不是进入System.Out文件。是否有人通过在ICN插件中使用Log4J库来配置自定义日志记录?我尝试按照Java程序中的方式完全配置,但是日志文件未在配置的目录中生成。这是

# Root logger option
log4j.rootLogger=INFO, file

# Redirect log messages to a log file, support file rolling.
log4j.appender.file=org.apache.log4j.RollingFileAppender
log4j.appender.file.File=/tmp/MyPlugin.log
log4j.appender.file.MaxFileSize=5MB
log4j.appender.file.MaxBackupIndex=10
log4j.appender.file.layout=org.apache.log4j.PatternLayout
log4j.appender.file.layout.ConversionPattern=%d{yyyy-MM-dd HH:mm:ss} %-5p %c{1}:%L - %m%n

我尝试将log4j.properties文件放入 / src文件夹甚至直接在 /文件夹下的log4j.properties文件。

有人可以用一些指针帮助我吗?

提前致谢..

Hi all IBM ICN Developers,

I am working on writing an ICN plugin. I want my logger statements to get logged into a separate file rather than going in System.Out file. Has anyone configured the custom logging by using Log4J libraries in an ICN Plugin? I tried configuring exactly as we do in a Java program but the log file is not getting generated in the configured directory. Here is my log4j.properties file

# Root logger option
log4j.rootLogger=INFO, file

# Redirect log messages to a log file, support file rolling.
log4j.appender.file=org.apache.log4j.RollingFileAppender
log4j.appender.file.File=/tmp/MyPlugin.log
log4j.appender.file.MaxFileSize=5MB
log4j.appender.file.MaxBackupIndex=10
log4j.appender.file.layout=org.apache.log4j.PatternLayout
log4j.appender.file.layout.ConversionPattern=%d{yyyy-MM-dd HH:mm:ss} %-5p %c{1}:%L - %m%n

I tried placing the log4j.properties file inside /src folder and even directly under / folder.

Can someone please help me with some pointers?

Thanks in advance..

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

戴着白色围巾的女孩 2025-02-03 07:39:48

您正在开发哪个版本的ICN。我认为(!)从3.0.9 IBM删除了log4j,以支持Jul(Java Utility Logging)。

希望这会有所帮助。

Which version of ICN are you developing on. I think(!) starting 3.0.9 IBM removed log4j in favor of JUL (Java Utility Logging).

Hope this helps.

春花秋月 2025-02-03 07:39:48

已解决。创建了一个单独的类,用于初始化Logger实例。从插件执行的开头调用的代码调用初始方法。

public class LoggingUtil {
    private static LoggingUtil instance = null;
    private static RollingFileAppender rfa = null;
    
    protected LoggingUtil() {
        
    }
    
    public static LoggingUtil getInstance() {
        if(instance == null) {
            instance = new LoggingUtil();
          }
          return instance;
    }
    
    public void init() {

        if (rfa == null) {

            try{
                LoggerContext context  = (LoggerContext)LogManager.getContext(false);
                File file = new File("//opt//log4j.xml");
                
                context.setConfigLocation(file.toURI());
                System.out.println("Log4J properties configured...");
            }catch(Exception e){
                System.out.println("ERROR occurred while loading properties file");
                e.printStackTrace();
            }
        }
    }
}

Solved. Created a separate class for initializing logger instance. The init method is called from the code which gets called in the beginning of the plugin execution.

public class LoggingUtil {
    private static LoggingUtil instance = null;
    private static RollingFileAppender rfa = null;
    
    protected LoggingUtil() {
        
    }
    
    public static LoggingUtil getInstance() {
        if(instance == null) {
            instance = new LoggingUtil();
          }
          return instance;
    }
    
    public void init() {

        if (rfa == null) {

            try{
                LoggerContext context  = (LoggerContext)LogManager.getContext(false);
                File file = new File("//opt//log4j.xml");
                
                context.setConfigLocation(file.toURI());
                System.out.println("Log4J properties configured...");
            }catch(Exception e){
                System.out.println("ERROR occurred while loading properties file");
                e.printStackTrace();
            }
        }
    }
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文