我在Java项目中使用Selenium WebDriver和log4j。我将log4j.properties设置如下:
# Define the root logger with appender file
log4j.rootLogger = DEBUG, stdout, FILE
# Define the file appender
log4j.appender.FILE=org.apache.log4j.FileAppender
# Set the name of the file
log4j.appender.FILE.File=C:/_privat/myproj/log/log.out
# Set the immediate flush to true (default)
log4j.appender.FILE.ImmediateFlush=true
# Set the threshold to debug mode
log4j.appender.FILE.Threshold=debug
# Set the append to false, overwrite
log4j.appender.FILE.Append=false
# Define the layout for file appender
log4j.appender.FILE.layout=org.apache.log4j.PatternLayout
log4j.appender.FILE.layout.conversionPattern=%m%n
# Redirect log messages to console
log4j.appender.stdout=org.apache.log4j.ConsoleAppender
log4j.appender.stdout.Target=System.out
log4j.appender.stdout.layout=org.apache.log4j.PatternLayout
log4j.appender.stdout.layout.ConversionPattern=%d{yyyy-MM-dd HH:mm:ss} %-5p [%-20c{1}:%3L] %m%n
log4j.appender.stdout.Threshold=INFO
在执行过程中,我仅看到这些行,而不是完整的日志。
Starting ChromeDriver 75.0.3770.8 (681f24ea911fe754973dda2fdc6d2a2e159dd300-refs/branch-heads/3770@{#40}) on port 3320
Only local connections are allowed.
Please protect ports used by ChromeDriver and related test frameworks to prevent access by malicious code.
jún. 14, 2019 8:26:53 DE org.openqa.selenium.remote.ProtocolHandshake createSession
INFO: Detected dialect: W3C
停止执行时,我还将所有日志消息也发送到控制台,该控制台消息已写入文件。在执行期间,我还需要在控制台中查看日志。