我已在我的代码中删除了所有system.out.println(),并添加了logache of apache log4j。但是在我的控制台中没有打印出来。这是我的log4j.properties文件
# Define the root logger with appender X
log4j.rootLogger = DEBUG, X
# Set the appender named X to be a File appender
log4j.appender.X=org.apache.log4j.FileAppender
# Define the layout for X appender
log4j.appender.X.layout=org.apache.log4j.PatternLayout
log4j.appender.X.layout.conversionPattern=%m%n
我按如下方式调用logger类
Logger log = Logger.getLogger(PersonImpl.class.getName());
除了我的log.info()之外,还会打印其他日志。
DEBUG [http-bio-8080-exec-3] (Table.java:505) - No alter strings for table : hibernate_sequence
DEBUG [http-bio-8080-exec-3] (JtaPlatformInitiator.java:42) - No JtaPlatform was specified, checking resolver
DEBUG [http-bio-8080-exec-3] (JtaPlatformResolverInitiator.java:33) - No JtaPlatformResolver was specified, using default [org.hibernate.engine.transaction.jta.platform.internal.StandardJtaPlatformResolver]
DEBUG [http-bio-8080-exec-3] (StandardJtaPlatformResolver.java:101) - Could not resolve JtaPlatform, using default [org.hibernate.engine.transaction.jta.platform.internal.NoJtaPlatform]
DEBUG [http-bio-8080-exec-3] (NamedQueryRepository.java:149) - Checking 0 named HQL queries
DEBUG [http-bio-8080-exec-3] (NamedQueryRepository.java:165) - Checking 0 named SQL queries
答案 0 :(得分:0)
您只添加了FileAppender
(这会导致日志仅在文件中写入),但如果您希望在控制台中完成日志记录,则还需要添加ConsoleAppender
。
可以按如下方式完成:
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 %c{1}:%L - %m%n
并在rootLogger中添加appender的名称,如下所示
log4j.rootLogger = DEBUG, X, stdout
除此之外,如果要关闭Hibernate特定日志,则必须执行
log4j.logger.org.hibernate=fatal or log4j.logger.org.hibernate=off
希望这有帮助!
祝你好运!答案 1 :(得分:0)
将此代码用于您希望实际生成日志的类。
private static final Logger LOGGER = Logger.getLogger("Class Name Where u used");
LOGGER.error ("string which u want to print");
LOGGER.info("string which u want to print");
LOGGER.warn("string which u want to print");
使用此设置我已在项目中执行此操作。它将生成日志到控制台以及logger.out到项目目录
log4j.rootLogger=INFO, FILE
# Define the file appender
log4j.appender.FILE=org.apache.log4j.RollingFileAppender
log4j.appender.FILE.File=logger.out
log4j.appender.FILE.MaxFileSize=1MB
log4j.appender.FILE.MaxBackupIndex=100
# Define the layout for file appender
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.appender.Console=org.apache.log4j.ConsoleAppender
log4j.appender.Console.layout=org.apache.log4j.PatternLayout
log4j.appender.Console.layout.conversionPattern=%m%n
乐意帮助!
答案 2 :(得分:0)
你可以在文件中打印日志
log4php.appender.services = LoggerAppenderDailyFile
log4php.appender.services.layout = LoggerLayoutPattern
log4php.appender.services.datePattern = Y-m-d
log4php.appender.services.file = / home / Logs_%s
log4php.appender.services.layout.ConversionPattern =“%24d {ISO8601} [%p]%F:%L%m%n”
log4php.rootLogger = debug,services
答案 3 :(得分:0)
试试这个:
log4j.rootLogger = DEBUG,stdout
log4j.appender.stdout = org.apache.log4j.ConsoleAppender
log4j.appender.stdout.Target = System.out的
log4j.appender.stdout.layout = org.apache.log4j.PatternLayout