gpt4 book ai didi

java - slf4j/logback FileAppender 在 eclipse 工作区中的编写

转载 作者:塔克拉玛干 更新时间:2023-11-03 05:32:05 27 4
gpt4 key购买 nike

我有一个 eclipse使用 slf4j 的 RCP 应用程序/logback捕获所有日志消息。

现在我配置了以下文件附加器:

<appender name="FILE" class="ch.qos.logback.core.FileAppender">
<file>softmodeler_client.log</file>
<append>true</append>
<encoder class="ch.qos.logback.classic.encoder.PatternLayoutEncoder">
<pattern>[%date] %level: %logger - %m%n</pattern>
</encoder>
</appender>

这会将日志文件写入应用程序安装目录。
一些客户不允许在安装目录中进行写访问。
在这种情况下,我们重定向工作区和配置目录,在 Applicationname.ini 文件中定义以下内容:

-data
@user.home/AppData/Roaming/Applicationname/workspace
-configuration
@user.home/AppData/Roaming/Applicationname/configuration

现在我想像这样配置 logback.xml,将日志文件指向可写工作区目录:

<file>${osgi.instance.area}softmodeler_client.log</file>

这会导致 FileNotFoundException,因为 osgi.instance.area 系统属性的 file:/ 前缀。我没有找到没有协议(protocol)的其他属性。

我的问题是如何配置 logback 写入我的工作区,而不必每次都修改 logback.xml

编辑:
也许我可以注册一个监听器,从那里我可以设置一些初始属性?
我更愿意使用现有的系统属性或以编程方式将变量传递给日志记录框架。目标是,不必为每个安装配置额外的参数/变量(因为相当多并且在更新期间可能会遗漏)。只是不知何故总是自动将日志文件写入工作区目录。

最佳答案

How can I configure logback to write into my workspace, without having to modify the logback.xml everytime?

我想建议你,如果任何用户想使用他自己的特定位置来写入他的日志文件,那么最好使用属性文件。

dataLocation.properties 文件有一个位置。它可能会因用户而异。

logpathfile = d://logFiles

然后将从java文件中读取该路径,并将其发送到logback.xml文件。


例如,我使用 sessionid 作为参数。它从 java 文件发送到 logback.xml。希望你能用你的代码调整它。

LoggerBySessionId.java

/**
* 2 * Logback: the reliable, generic, fast and flexible logging framework.
* 3 * Copyright (C) 1999-2015, QOS.ch. All rights reserved.
* 4 *
* 5 * This program and the accompanying materials are dual-licensed under
* 6 * either the terms of the Eclipse Public License v1.0 as published by
* 7 * the Eclipse Foundation
* 8 *
* 9 * or (per the licensee's choosing)
* 10 *
* 11 * under the terms of the GNU Lesser General Public License version 2.1
* 12 * as published by the Free Software Foundation.
* 13
*/
package com.waze.rr_logger;

import ch.qos.logback.classic.LoggerContext;
import ch.qos.logback.classic.joran.JoranConfigurator;
import ch.qos.logback.core.joran.spi.JoranException;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.slf4j.MDC;

public class LoggerBySessionId {

String configFile = "logback.xml";

public LoggerBySessionId() {
}

public LoggerBySessionId(String configFile) {
this.configFile = configFile;
}

public void log(String sessionId, String content) throws JoranException {

LoggerContext lc = (LoggerContext) LoggerFactory.getILoggerFactory();
JoranConfigurator configurator = new JoranConfigurator();
lc.reset();
configurator.setContext(lc);
configurator.doConfigure(configFile);

Logger logger = LoggerFactory.getLogger(LoggerBySessionId.class);
MDC.put("sessionId", sessionId); // this sessionId is sent to logback.xml
logger.debug(content);
}

static void usage(String msg) {
System.err.println(msg);
System.err.println("Usage: java " + LoggerBySessionId.class.getName() + " configFile\n" + " configFile a logback " +
"configuration file");
System.exit(1);
}
}

logback.xml

<configuration>
<appender name="SIFT" class="ch.qos.logback.classic.sift.SiftingAppender">
<!-- in the absence of the class attribute, it is assumed that the
desired discriminator type is
ch.qos.logback.classic.sift.MDCBasedDiscriminator -->
<discriminator>
<key>userid</key>
<defaultValue>no_session_id</defaultValue>
</discriminator>
<sift>
<appender name="FILE-${sessionId}" class="ch.qos.logback.core.FileAppender">
<file>${userid}.log</file>
<append>true</append>
<layout class="ch.qos.logback.classic.PatternLayout">
<pattern>%d [%thread] %level %mdc %logger{35} - %msg%n</pattern>
</layout>
</appender>
<appender name="FILE-${sessionId}" class="ch.qos.logback.core.FileAppender">
<file>${userid}.log</file>
<append>false</append>
<layout class="ch.qos.logback.classic.PatternLayout">
<pattern>%d [%thread] %level %mdc %logger{35} - %msg%n</pattern>
</layout>
</appender>
</sift>
</appender>

<root level="DEBUG">
<appender-ref ref="SIFT" />
</root>
</configuration>

关于java - slf4j/logback FileAppender 在 eclipse 工作区中的编写,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32200038/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com