我想记录" ch.qos.logback"使用Logback将类分类到日志文件中,但它只是在控制台中而不是在文件中记录。
有可能吗?
我需要它来调查一些logback问题。
这是我的logback配置文件:
//: Playground - noun: a place where people can play
import MultipeerConnectivity
import PlaygroundSupport
class Client: NSObject, MCNearbyServiceBrowserDelegate {
let serviceBrowser: MCNearbyServiceBrowser
override init() {
serviceBrowser = MCNearbyServiceBrowser(peer: MCPeerID(displayName: "client"), serviceType: "MCTest")
super.init()
serviceBrowser.delegate = self
serviceBrowser.startBrowsingForPeers()
}
func browser(_ browser: MCNearbyServiceBrowser, foundPeer peerID: MCPeerID, withDiscoveryInfo info: [String : String]?) {
NSLog("foundPeer: \(peerID)")
}
func browser(_ browser: MCNearbyServiceBrowser, lostPeer peerID: MCPeerID) {
NSLog("lostPeer: \(peerID)")
}
}
let myClient = Client()
PlaygroundPage.current.needsIndefiniteExecution = true
这是登录控制台的示例,我希望将其放在文件中,而不仅仅是在控制台中。
<font>
我正在使用此依赖项:
<?xml version="1.0" encoding="ISO-8859-1"?>
<configuration scan="true" scanPeriod="60 seconds">
<property name="base-path" value="../../xpto/sysX/logs"/>
<property name="application-name" value="app_X"/>
<appender class="ch.qos.logback.core.ConsoleAppender" name="CONSOLE">
<param name="Threshold" value="INFO"/>
<encoder>
<pattern>%d{dd/MM/yyyy HH:mm:ss.SSS} %-5level %logger{30} - %msg%n</pattern>
</encoder>
</appender>
<appender class="ch.qos.logback.core.rolling.RollingFileAppender" name="FILE_LOGBACK">
<param name="Threshold" value="DEBUG"/>
<file>${base-path}/mylog.log</file>
<rollingPolicy class="ch.qos.logback.core.rolling.TimeBasedRollingPolicy">
<fileNamePattern>${base-path}/%d{yyyy-MM-dd_HH}/mylog.%i.log</fileNamePattern>
<maxHistory>72</maxHistory>
<timeBasedFileNamingAndTriggeringPolicy class="ch.qos.logback.core.rolling.SizeAndTimeBasedFNATP">
<maxFileSize>20MB</maxFileSize>
</timeBasedFileNamingAndTriggeringPolicy>
</rollingPolicy>
<encoder>
<pattern>%date{yyyy-MM-dd HH:mm:ss.SSS} [%.30thread] %-5level %logger{50} - %msg%n</pattern>
</encoder>
</appender>
<appender class="ch.qos.logback.classic.AsyncAppender" name="FILE_LOGBACK_ASYNC">
<param name="Threshold" value="DEBUG"/>
<appender-ref ref="FILE_LOGBACK"/>
<queueSize>1000</queueSize>
<discardingThreshold>0</discardingThreshold>
</appender>
<logger additivity="false" level="DEBUG" name="ch.qos.logback">
<appender-ref ref="FILE_LOGBACK"/>
</logger>
<root level="INFO">
<appender-ref ref="CONSOLE"/>
<appender-ref ref="FILE_LOGBACK_ASYNC"/>
</root>
</configuration>
提前致谢
答案 0 :(得分:3)
对于背景,这个输出......
14:17:37,117 |-INFO in ch.qos.logback.core.joran.action.AppenderAction - Naming appender as [FILE_LOGBACK]
14:17:37,117 |-WARN in ch.qos.logback.core.joran.util.PropertySetter@7f7f557 - No setter for property [Threshold] in ch.qos.logb ack.core.rolling.RollingFileAppender.
14:17:37,118 |-INFO in c.q.l.core.rolling.TimeBasedRollingPolicy@671885015 - No compression will be used
14:17:37,118 |-INFO in c.q.l.core.rolling.TimeBasedRollingPolicy@671885015 - Will use the pattern ../../xpto/sysX/logs/%d{ yyyy-MM-dd_HH}/mylog.%i.log for the active file
14:17:37,119 |-INFO in ch.qos.logback.core.rolling.SizeAndTimeBasedFNATP@35ca01cb - The date pattern is 'yyyy-MM-dd_HH' from fil e name pattern '.../../xpto/sysX/logs/%d{yyyy-MM-dd_HH}/mylog.%i.log'.
14:17:37,119 |-INFO in ch.qos.logback.core.rolling.SizeAndTimeBasedFNATP@35ca01cb - Roll-over at the top of every hour.
14:17:37,119 |-INFO in ch.qos.logback.core.rolling.SizeAndTimeBasedFNATP@35ca01cb - Setting initial period to Mon Oct 02 14:17:3
...由Logback在以下条件下发出:
<configuration debug="true">...</configuration>
这些日志事件是在使用您的配置初始化Logback之前发出的,因此在它们被发出时,Logback对您配置的appender等一无所知。这些事件的唯一安全目标是Logback创建的ConsoleAppender
这个目的。
因此,没有办法告诉Logback将自己的on-startup-log-events指向文件追加器。
但是,你真的想要这个Logback输出吗?如果没有,那么您可以通过......
来抑制它debug=false
并且没有ERROR / WARN事件,并且类路径上有一个Logback配置文件<statusListener class="ch.qos.logback.core.status.NopStatusListener" />
如果您确实需要此输出,那么您可以......
System.setOut(new PrintStream("/Users/al/Projects/Sarah2/std.out"));
java -jar ... > /some/directory/application_stdout.log
ch.qos.logback.core.status.StatusListener
实施(而非NopStatusListener
),以便将这些状态事件写入您选择的文件ch.qos.logback.core.status.StatusListenerAsList
(而不是NopStatusListener
),然后向您的应用添加一些内容,将getStatusList()
的内容写入您选择的文件答案 1 :(得分:1)
您可以编写自己的StatusListener来写入文件。我在那里找不到很多例子,但是如果你看一下OnConsoleStatusListener的源代码,那么把它们放在那里并修改以写入文件是非常简单的。
import ch.qos.logback.core.status.Status;
import ch.qos.logback.core.status.StatusListener;
import ch.qos.logback.core.util.StatusPrinter;
import java.io.BufferedWriter;
import java.io.FileWriter;
import java.io.IOException;
public class FileStatusListener implements StatusListener {
private final static String fileName = "path/logbackStatusMessages.log";
@Override
public void addStatusEvent(Status status) {
StringBuilder sb = new StringBuilder();
StatusPrinter.buildStr(sb, "", status);
BufferedWriter writer = null;
try {
writer = new BufferedWriter(new FileWriter(fileName, true));
writer.append(sb.toString());
} catch (Throwable ex) {
System.out.println("Error writing to file in FileStatusListener");
ex.printStackTrace(System.err);
} finally {
if (writer != null) {
try {
writer.close();
} catch (IOException ex) {
System.err.println("Error closing file in FileStatusListener");
ex.printStackTrace(System.err);
}
}
}
}
}
然后在配置文件中将您的类指定为Status Listener类:
<statusListener class="FileStatusListener" />
当然,您可能希望使代码更健壮,但写入滚动文件。
编辑:
更好的是,OnPrintStreamStatusListenerBase是一个抽象类,它为写入文件提供了更强大的框架。它可能是比上面的代码更好的选择。