log4j -> logback

This commit is contained in:
bgamard 2023-04-12 12:16:04 +02:00
parent 95e0b870f6
commit 97d6e8f528
18 changed files with 180 additions and 169 deletions

View File

@ -62,16 +62,6 @@
<artifactId>jsoup</artifactId>
</dependency>
<dependency>
<groupId>log4j</groupId>
<artifactId>log4j</artifactId>
</dependency>
<dependency>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-log4j12</artifactId>
</dependency>
<dependency>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-api</artifactId>
@ -82,6 +72,21 @@
<artifactId>jcl-over-slf4j</artifactId>
</dependency>
<dependency>
<groupId>org.slf4j</groupId>
<artifactId>jul-to-slf4j</artifactId>
</dependency>
<dependency>
<groupId>org.apache.logging.log4j</groupId>
<artifactId>log4j-to-slf4j</artifactId>
</dependency>
<dependency>
<groupId>ch.qos.logback</groupId>
<artifactId>logback-classic</artifactId>
</dependency>
<dependency>
<groupId>at.favre.lib</groupId>
<artifactId>bcrypt</artifactId>

View File

@ -1,7 +1,8 @@
package com.sismics.util.io;
import com.google.common.io.Closer;
import org.apache.log4j.Logger;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import java.io.BufferedReader;
import java.io.IOException;
@ -15,7 +16,7 @@ import java.io.InputStreamReader;
*/
public class InputStreamReaderThread extends Thread {
private static final Logger logger = Logger.getLogger(InputStreamReaderThread.class);
private static final Logger logger = LoggerFactory.getLogger(InputStreamReaderThread.class);
private InputStream is;

View File

@ -1,7 +1,7 @@
package com.sismics.util.log4j;
package com.sismics.util.logback;
import ch.qos.logback.classic.Level;
import org.apache.commons.lang3.StringUtils;
import org.apache.log4j.Level;
/**
* Log search criteria.

View File

@ -1,6 +1,7 @@
package com.sismics.util.log4j;
package com.sismics.util.logback;
import org.apache.log4j.Level;
import ch.qos.logback.classic.Level;
/**
* Log entry.

View File

@ -1,25 +1,22 @@
package com.sismics.util.log4j;
package com.sismics.util.logback;
import ch.qos.logback.classic.Level;
import ch.qos.logback.classic.spi.ILoggingEvent;
import ch.qos.logback.core.AppenderBase;
import com.google.common.collect.Lists;
import com.sismics.docs.core.util.jpa.PaginatedList;
import java.util.Iterator;
import java.util.LinkedList;
import java.util.List;
import java.util.Queue;
import java.util.concurrent.ConcurrentLinkedQueue;
import org.apache.log4j.AppenderSkeleton;
import org.apache.log4j.Level;
import org.apache.log4j.helpers.LogLog;
import org.apache.log4j.spi.LoggingEvent;
import com.google.common.collect.Lists;
import com.sismics.docs.core.util.jpa.PaginatedList;
/**
* Memory appender for Log4J.
* Memory appender for Logback.
*
* @author jtremeaux
*/
public class MemoryAppender extends AppenderSkeleton {
public class MemoryAppender extends AppenderBase<ILoggingEvent> {
/**
* Maximum size of the queue.
@ -29,34 +26,18 @@ public class MemoryAppender extends AppenderSkeleton {
/**
* Queue of log entries.
*/
private final Queue<LogEntry> logQueue = new ConcurrentLinkedQueue<LogEntry>();
private static final Queue<LogEntry> logQueue = new ConcurrentLinkedQueue<>();
@Override
public boolean requiresLayout() {
return false;
}
@Override
public synchronized void close() {
if (closed) {
return;
}
closed = true;
}
@Override
public synchronized void append(LoggingEvent event) {
protected void append(ILoggingEvent event) {
while (logQueue.size() > size) {
logQueue.remove();
}
if (closed) {
LogLog.warn("This appender is already closed, cannot append event.");
return;
}
String loggerName = getLoggerName(event);
LogEntry logEntry = new LogEntry(System.currentTimeMillis(), event.getLevel(), loggerName, event.getMessage().toString());
LogEntry logEntry = new LogEntry(System.currentTimeMillis(), event.getLevel(), loggerName, event.getMessage());
logQueue.add(logEntry);
}
@ -66,7 +47,7 @@ public class MemoryAppender extends AppenderSkeleton {
* @param event Event
* @return Class name
*/
private String getLoggerName(LoggingEvent event) {
private String getLoggerName(ILoggingEvent event) {
int index = event.getLoggerName().lastIndexOf('.');
return (index > -1) ?
@ -75,12 +56,12 @@ public class MemoryAppender extends AppenderSkeleton {
}
/**
* Getter of logList.
* Getter of size.
*
* @return logList
* @return size
*/
public Queue<LogEntry> getLogList() {
return logQueue;
public int getSize() {
return size;
}
/**
@ -98,15 +79,14 @@ public class MemoryAppender extends AppenderSkeleton {
* @param criteria Search criteria
* @param list Paginated list (modified by side effect)
*/
public void find(LogCriteria criteria, PaginatedList<LogEntry> list) {
public static void find(LogCriteria criteria, PaginatedList<LogEntry> list) {
List<LogEntry> logEntryList = new LinkedList<LogEntry>();
final Level minLevel = criteria.getMinLevel();
final String tag = criteria.getTag();
final String message = criteria.getMessage();
int resultCount = 0;
for (Iterator<LogEntry> it = logQueue.iterator(); it.hasNext();) {
LogEntry logEntry = it.next();
if ((minLevel == null || Integer.compare(logEntry.getLevel().toInt(), minLevel.toInt()) >= 0) &&
for (LogEntry logEntry : logQueue) {
if ((minLevel == null || logEntry.getLevel().toInt() >= minLevel.toInt()) &&
(tag == null || logEntry.getTag().toLowerCase().equals(tag)) &&
(message == null || logEntry.getMessage().toLowerCase().contains(message))) {
logEntryList.add(logEntry);

View File

@ -1,10 +0,0 @@
log4j.rootCategory=DEBUG, CONSOLE, MEMORY
log4j.appender.CONSOLE=org.apache.log4j.ConsoleAppender
log4j.appender.CONSOLE.layout=org.apache.log4j.PatternLayout
log4j.appender.CONSOLE.layout.ConversionPattern=%d{DATE} %p %l %m %n
log4j.appender.MEMORY=com.sismics.util.log4j.MemoryAppender
log4j.appender.MEMORY.size=1000
log4j.logger.com.sismics=INFO
log4j.logger.org.hibernate=ERROR
log4j.logger.org.apache.directory=ERROR

View File

@ -0,0 +1,18 @@
<?xml version="1.0" encoding="UTF-8"?>
<configuration>
<appender name="CONSOLE" class="ch.qos.logback.core.ConsoleAppender">
<encoder>
<pattern>%date [%t] %-5level %logger{36} - %msg%n</pattern>
</encoder>
</appender>
<appender name="MEMORY" class="com.sismics.util.logback.MemoryAppender">
<size>1000</size>
</appender>
<logger name="org.apache.directory" level="ERROR"/>
<logger name="com.sismics" level="INFO"/>
<logger name="org.hibernate" level="ERROR"/>
<root level="DEBUG">
<appender-ref ref="CONSOLE"/>
<appender-ref ref="MEMORY"/>
</root>
</configuration>

View File

@ -48,11 +48,6 @@
<artifactId>guava</artifactId>
</dependency>
<dependency>
<groupId>log4j</groupId>
<artifactId>log4j</artifactId>
</dependency>
<dependency>
<groupId>jakarta.servlet</groupId>
<artifactId>jakarta.servlet-api</artifactId>
@ -64,11 +59,6 @@
<artifactId>joda-time</artifactId>
</dependency>
<dependency>
<groupId>org.slf4j</groupId>
<artifactId>jul-to-slf4j</artifactId>
</dependency>
<!-- Test dependencies -->
<dependency>
<groupId>junit</groupId>

View File

@ -1,23 +1,26 @@
package com.sismics.util.filter;
import ch.qos.logback.classic.Level;
import ch.qos.logback.classic.LoggerContext;
import ch.qos.logback.classic.encoder.PatternLayoutEncoder;
import ch.qos.logback.classic.spi.ILoggingEvent;
import ch.qos.logback.core.rolling.RollingFileAppender;
import ch.qos.logback.core.rolling.SizeAndTimeBasedRollingPolicy;
import ch.qos.logback.core.util.FileSize;
import com.sismics.docs.core.model.context.AppContext;
import com.sismics.docs.core.util.DirectoryUtil;
import com.sismics.docs.core.util.TransactionUtil;
import com.sismics.util.EnvironmentUtil;
import com.sismics.util.context.ThreadLocalContext;
import com.sismics.util.jpa.EMF;
import jakarta.persistence.EntityManager;
import jakarta.persistence.EntityTransaction;
import jakarta.servlet.*;
import jakarta.servlet.http.HttpServletResponse;
import jakarta.ws.rs.core.HttpHeaders;
import org.apache.log4j.Level;
import org.apache.log4j.PatternLayout;
import org.apache.log4j.RollingFileAppender;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.slf4j.bridge.SLF4JBridgeHandler;
import jakarta.persistence.EntityManager;
import jakarta.persistence.EntityTransaction;
import java.io.IOException;
import java.text.MessageFormat;
@ -48,18 +51,30 @@ public class RequestContextFilter implements Filter {
// Initialize file logger
RollingFileAppender fileAppender = new RollingFileAppender();
fileAppender.setName("FILE");
fileAppender.setFile(DirectoryUtil.getLogDirectory().resolve("docs.log").toString());
fileAppender.setLayout(new PatternLayout("%d{DATE} %p %l %m %n"));
fileAppender.setThreshold(Level.INFO);
fileAppender.setAppend(true);
fileAppender.setMaxFileSize("5MB");
fileAppender.setMaxBackupIndex(5);
fileAppender.activateOptions();
org.apache.log4j.Logger.getRootLogger().addAppender(fileAppender);
SLF4JBridgeHandler.removeHandlersForRootLogger();
SLF4JBridgeHandler.install();
LoggerContext logContext = (LoggerContext) LoggerFactory.getILoggerFactory();
RollingFileAppender<ILoggingEvent> appender = new RollingFileAppender<>();
PatternLayoutEncoder encoder = new PatternLayoutEncoder();
encoder.setContext(logContext);
encoder.setPattern("%date [%t] %-5level %logger{36} - %msg%n");
encoder.start();
SizeAndTimeBasedRollingPolicy<ILoggingEvent> rollingPolicy = new SizeAndTimeBasedRollingPolicy<>();
rollingPolicy.setMaxFileSize(FileSize.valueOf("5MB"));
rollingPolicy.setFileNamePattern("docs.%d{yyyy-MM-dd_HH}.log");
rollingPolicy.setMaxHistory(5);
rollingPolicy.setContext(logContext);
rollingPolicy.setParent(appender);
rollingPolicy.start();
appender.setContext(logContext);
appender.setName("FILE");
appender.setFile(DirectoryUtil.getLogDirectory().resolve("docs.log").toString());
appender.setEncoder(encoder);
appender.setRollingPolicy(rollingPolicy);
appender.setAppend(true);
appender.start();
ch.qos.logback.classic.Logger logger = logContext.getLogger(Logger.ROOT_LOGGER_NAME);
logger.setAdditive(false);
logger.setLevel(Level.INFO);
logger.addAppender(appender);
// Initialize the application context
TransactionUtil.handle(AppContext::getInstance);

View File

@ -63,11 +63,6 @@
<artifactId>guava</artifactId>
</dependency>
<dependency>
<groupId>log4j</groupId>
<artifactId>log4j</artifactId>
</dependency>
<dependency>
<groupId>joda-time</groupId>
<artifactId>joda-time</artifactId>

View File

@ -1,11 +0,0 @@
log4j.rootCategory=WARN, CONSOLE, MEMORY
log4j.appender.CONSOLE=org.apache.log4j.ConsoleAppender
log4j.appender.CONSOLE.layout=org.apache.log4j.PatternLayout
log4j.appender.CONSOLE.layout.ConversionPattern=%d{DATE} %p %l %m %n
log4j.appender.MEMORY=com.sismics.util.log4j.MemoryAppender
log4j.appender.MEMORY.size=1000
log4j.logger.com.sismics=DEBUG
log4j.logger.org.apache.pdfbox=ERROR
log4j.logger.org.glassfish.jersey.servlet.WebComponent=ERROR
log4j.logger.org.apache.directory=ERROR

View File

@ -0,0 +1,19 @@
<?xml version="1.0" encoding="UTF-8"?>
<configuration>
<appender name="CONSOLE" class="ch.qos.logback.core.ConsoleAppender">
<encoder>
<pattern>%date [%t] %-5level %logger{36} - %msg%n</pattern>
</encoder>
</appender>
<appender name="MEMORY" class="com.sismics.util.logback.MemoryAppender">
<size>1000</size>
</appender>
<logger name="org.apache.directory" level="ERROR"/>
<logger name="com.sismics" level="DEBUG"/>
<logger name="org.apache.pdfbox" level="ERROR"/>
<logger name="org.glassfish.jersey.servlet.WebComponent" level="ERROR"/>
<root level="WARN">
<appender-ref ref="CONSOLE"/>
<appender-ref ref="MEMORY"/>
</root>
</configuration>

View File

@ -1,5 +1,6 @@
package com.sismics.docs.rest.resource;
import ch.qos.logback.classic.Level;
import com.google.common.base.Strings;
import com.sismics.docs.core.constant.ConfigType;
import com.sismics.docs.core.constant.Constants;
@ -23,9 +24,9 @@ import com.sismics.rest.exception.ServerException;
import com.sismics.rest.util.ValidationUtil;
import com.sismics.util.JsonUtil;
import com.sismics.util.context.ThreadLocalContext;
import com.sismics.util.log4j.LogCriteria;
import com.sismics.util.log4j.LogEntry;
import com.sismics.util.log4j.MemoryAppender;
import com.sismics.util.logback.LogCriteria;
import com.sismics.util.logback.LogEntry;
import com.sismics.util.logback.MemoryAppender;
import jakarta.json.Json;
import jakarta.json.JsonArrayBuilder;
import jakarta.json.JsonObjectBuilder;
@ -34,8 +35,6 @@ import jakarta.persistence.Query;
import jakarta.ws.rs.*;
import jakarta.ws.rs.core.Response;
import org.apache.commons.lang3.StringUtils;
import org.apache.log4j.Appender;
import org.apache.log4j.Level;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
@ -539,14 +538,6 @@ public class AppResource extends BaseResource {
}
checkBaseFunction(BaseFunction.ADMIN);
// Get the memory appender
org.apache.log4j.Logger logger = org.apache.log4j.Logger.getRootLogger();
Appender appender = logger.getAppender("MEMORY");
if (!(appender instanceof MemoryAppender)) {
throw new ServerException("ServerError", "MEMORY appender not configured");
}
MemoryAppender memoryAppender = (MemoryAppender) appender;
// Find the logs
LogCriteria logCriteria = new LogCriteria()
.setMinLevel(Level.toLevel(StringUtils.stripToNull(minLevel)))
@ -554,7 +545,7 @@ public class AppResource extends BaseResource {
.setMessage(StringUtils.stripToNull(message));
PaginatedList<LogEntry> paginatedList = PaginatedLists.create(limit, offset);
memoryAppender.find(logCriteria, paginatedList);
MemoryAppender.find(logCriteria, paginatedList);
JsonArrayBuilder logs = Json.createArrayBuilder();
for (LogEntry logEntry : paginatedList.getResultList()) {
logs.add(Json.createObjectBuilder()

View File

@ -1,12 +0,0 @@
log4j.rootCategory=WARN, CONSOLE, MEMORY
log4j.appender.CONSOLE=org.apache.log4j.ConsoleAppender
log4j.appender.CONSOLE.layout=org.apache.log4j.PatternLayout
log4j.appender.CONSOLE.layout.ConversionPattern=%d{DATE} %p %l %m %n
log4j.appender.MEMORY=com.sismics.util.log4j.MemoryAppender
log4j.appender.MEMORY.size=1000
log4j.logger.com.sismics=INFO
log4j.logger.org.apache.pdfbox=ERROR
log4j.logger.org.glassfish.jersey.servlet.WebComponent=ERROR
log4j.logger.org.apache.directory=ERROR
log4j.logger.org.odftoolkit=ERROR

View File

@ -0,0 +1,20 @@
<?xml version="1.0" encoding="UTF-8"?>
<configuration>
<appender name="CONSOLE" class="ch.qos.logback.core.ConsoleAppender">
<encoder>
<pattern>%date [%t] %-5level %logger{36} - %msg%n</pattern>
</encoder>
</appender>
<appender name="MEMORY" class="com.sismics.util.logback.MemoryAppender">
<size>1000</size>
</appender>
<logger name="org.apache.directory" level="ERROR"/>
<logger name="com.sismics" level="INFO"/>
<logger name="org.apache.pdfbox" level="ERROR"/>
<logger name="org.glassfish.jersey.servlet.WebComponent" level="ERROR"/>
<logger name="org.odftoolkit" level="ERROR"/>
<root level="WARN">
<appender-ref ref="CONSOLE"/>
<appender-ref ref="MEMORY"/>
</root>
</configuration>

View File

@ -1,15 +0,0 @@
log4j.rootCategory=INFO, CONSOLE, MEMORY
log4j.appender.CONSOLE=org.apache.log4j.ConsoleAppender
log4j.appender.CONSOLE.layout=org.apache.log4j.PatternLayout
log4j.appender.CONSOLE.layout.ConversionPattern=%d{DATE} %p %l %m %n
log4j.appender.MEMORY=com.sismics.util.log4j.MemoryAppender
log4j.appender.MEMORY.size=1000
log4j.logger.com.sismics=INFO
log4j.logger.com.sismics.util.jpa=ERROR
log4j.logger.org.hibernate=ERROR
log4j.logger.org.apache.pdfbox=INFO
log4j.logger.com.mchange=ERROR
log4j.logger.org.apache.directory=ERROR
log4j.logger.org.glassfish.grizzly=ERROR
log4j.logger.org.odftoolkit=ERROR

View File

@ -0,0 +1,23 @@
<?xml version="1.0" encoding="UTF-8"?>
<configuration>
<appender name="CONSOLE" class="ch.qos.logback.core.ConsoleAppender">
<encoder>
<pattern>%date [%t] %-5level %logger{36} - %msg%n</pattern>
</encoder>
</appender>
<appender name="MEMORY" class="com.sismics.util.logback.MemoryAppender">
<size>1000</size>
</appender>
<logger name="org.apache.directory" level="ERROR"/>
<logger name="com.sismics" level="INFO"/>
<logger name="org.glassfish.grizzly" level="ERROR"/>
<logger name="org.apache.pdfbox" level="INFO"/>
<logger name="org.hibernate" level="ERROR"/>
<logger name="com.mchange" level="ERROR"/>
<logger name="com.sismics.util.jpa" level="ERROR"/>
<logger name="org.odftoolkit" level="ERROR"/>
<root level="INFO">
<appender-ref ref="CONSOLE"/>
<appender-ref ref="MEMORY"/>
</root>
</configuration>

33
pom.xml
View File

@ -22,10 +22,11 @@
<org.apache.commons.commons-email.version>1.5</org.apache.commons.commons-email.version>
<org.freemarker.freemarker.version>2.3.32</org.freemarker.freemarker.version>
<com.google.guava.guava.version>31.1-jre</com.google.guava.guava.version>
<log4j.log4j.version>1.2.17</log4j.log4j.version>
<org.slf4j.version>1.7.30</org.slf4j.version>
<org.slf4j.jcl-over-slf4j.version>1.7.30</org.slf4j.jcl-over-slf4j.version>
<org.slf4j.jul-to-slf4j.version>1.7.30</org.slf4j.jul-to-slf4j.version>
<org.slf4j.version>2.0.7</org.slf4j.version>
<org.slf4j.jcl-over-slf4j.version>2.0.7</org.slf4j.jcl-over-slf4j.version>
<org.slf4j.jul-to-slf4j.version>2.0.7</org.slf4j.jul-to-slf4j.version>
<org.apache.logging.log4j.log4j-to-slf4j.version>2.20.0</org.apache.logging.log4j.log4j-to-slf4j.version>
<ch.qos.logback.logback-classic.version>1.4.6</ch.qos.logback.logback-classic.version>
<junit.junit.version>4.13.2</junit.junit.version>
<com.h2database.h2.version>1.4.199</com.h2database.h2.version>
<jakarta.json.jakarta.json-api.version>2.1.1</jakarta.json.jakarta.json-api.version>
@ -217,18 +218,6 @@
<version>${com.squareup.okhttp3.okhttp.version}</version>
</dependency>
<dependency>
<groupId>log4j</groupId>
<artifactId>log4j</artifactId>
<version>${log4j.log4j.version}</version>
</dependency>
<dependency>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-log4j12</artifactId>
<version>${org.slf4j.version}</version>
</dependency>
<dependency>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-api</artifactId>
@ -247,6 +236,18 @@
<version>${org.slf4j.jul-to-slf4j.version}</version>
</dependency>
<dependency>
<groupId>org.apache.logging.log4j</groupId>
<artifactId>log4j-to-slf4j</artifactId>
<version>${org.apache.logging.log4j.log4j-to-slf4j.version}</version>
</dependency>
<dependency>
<groupId>ch.qos.logback</groupId>
<artifactId>logback-classic</artifactId>
<version>${ch.qos.logback.logback-classic.version}</version>
</dependency>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>