我有一個簡單的應用程序networked knock-knock joke app。我將一些Log4J(版本2)登錄到它。這裏是服務器類:如何在此應用程序中使Apache Log4J日誌記錄有用?
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import org.apache.logging.log4j.Level;
import java.net.*;
import java.io.*;
public class MessageResponseServer extends Thread /* added in the T just now*/{ /* REPLACED */
private static final Logger logger = LogManager.getLogger("MessageResponseServer");
logger.info("MessageResponseServer.java : INFO message");
public static void main(String[] args) throws IOException {
logger.debug("MessageResponseServer.java : DEBUG message");
ServerSocket serverSocket = null;
try {
serverSocket = new ServerSocket(4444);
} catch (IOException e) {
System.err.println("Could not listen on port: 4444.");
logger.fatal("MessageResponseServer.java : FATAL message - Could not listen on port: 4444.");
System.exit(1);
}
Socket clientSocket = null;
try {
clientSocket = serverSocket.accept();
logger.debug("MessageResponseServer.java : , debug message");
} catch (IOException e) {
System.err.println("Accept failed.");
System.exit(1);
}
PrintWriter out = new PrintWriter(clientSocket.getOutputStream(), true);
BufferedReader in = new BufferedReader(
new InputStreamReader(
clientSocket.getInputStream()));
String inputLine, outputLine;
MessageResponseProtocol mrp = new MessageResponseProtocol(); /* REPLACED */
outputLine = mrp.processInput(null);
out.println(outputLine);
while ((inputLine = in.readLine()) != null) {
outputLine = mrp.processInput(inputLine);
out.println(outputLine);
if (outputLine.equals("Bye."))
logger.debug("MessageResponseServer.java : , Exiting. DEBUG Message");
break;
}
out.close();
in.close();
clientSocket.close();
serverSocket.close();
}
}
而下面是XML文件:
<?xml version="1.0" encoding="UTF-8"?>
<Configuration status="WARN">
<Appenders>
<Console name="Console" target="SYSTEM_OUT">
<PatternLayout pattern="%d{HH:mm:ss} [%t] %-5level %logger{36} - %msg%n"/>
</Console>
<File name="MyFile" fileName="OutputLogFile.log" immediateFlush="false" append="true">
<PatternLayout pattern="%d{yyy-MM-dd HH:mm:ss.SSS} [%t] %-5level %logger{36} - %msg%n"/>
</File>
</Appenders>
<Loggers>
<Root level="ALL">
<Appender-Ref ref="Console"/>
<Appender-Ref ref="MyFile"/>
</Root>
</Loggers>
</Configuration>
我想要做的是找出如何使登錄多一點有用的。您是否添加了特殊的if
語句來決定是否記錄某些內容(即,如果用戶輸入「quit」,我可以對此進行特定的登錄)。
有沒有可能將性能指標納入日誌的方法?這對我來說真的很有用。我的目標是讓代碼演示一些可能有助於稍後展示故障安全功能的內容(也就是說,如果它被中止,我們可能會利用日誌來重新啓動客戶端)。
感謝
確定我想出與XML的文件的方式:'<的PatternLayout圖案=「%d {YYY-MM-DD HH:MM :ss.SSS}行:%L [%t]%-5級別記錄器{36} - %msg%n「/>' – Coffee