Skip to content

Commit

Permalink
Fixes #4292 - Refactor LoggingFeature to do async logging (#4293)
Browse files Browse the repository at this point in the history
  • Loading branch information
mnriem authored Nov 27, 2024
1 parent ff1b2d4 commit 731ce16
Show file tree
Hide file tree
Showing 5 changed files with 133 additions and 6 deletions.
2 changes: 1 addition & 1 deletion .mvn/maven.config
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
--show-version
-T
8
4
Original file line number Diff line number Diff line change
Expand Up @@ -27,12 +27,14 @@
*/
package cloud.piranha.feature.logging;

import cloud.piranha.feature.impl.DefaultFeature;
import java.util.logging.ConsoleHandler;
import java.util.logging.Handler;
import java.util.logging.Level;
import java.util.logging.LogManager;
import java.util.logging.Logger;

import cloud.piranha.feature.impl.DefaultFeature;

/**
* The Logging feature.
*
Expand Down Expand Up @@ -61,10 +63,29 @@ public String getLevel() {

@Override
public void init() {
/*
* Remote the default console handler.
*/
Logger rootLogger = Logger.getLogger("");
Handler[] handlers = rootLogger.getHandlers();
if (handlers[0] instanceof ConsoleHandler) {
rootLogger.removeHandler(handlers[0]);
}

/*
* Add our custom console handler.
*/
LoggingHandler loggingHandler = new LoggingHandler();
rootLogger.addHandler(loggingHandler);
rootLogger.setLevel(Level.ALL);

/**
* Set the log level (if set).
*/
if (level != null) {
Logger logger = LogManager.getLogManager().getLogger("");
logger.setLevel(Level.parse(level));
Handler[] handlers = logger.getHandlers();
handlers = logger.getHandlers();
for(int i=0; i<handlers.length; i++) {
handlers[i].setLevel(Level.parse(level));
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,105 @@
/*
* Copyright (c) 2002-2024 Manorrock.com. All Rights Reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions are met:
*
* 1. Redistributions of source code must retain the above copyright notice,
* this list of conditions and the following disclaimer.
* 2. Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
* 3. Neither the name of the copyright holder nor the names of its
* contributors may be used to endorse or promote products derived from
* this software without specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
* AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
* ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
* LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
* CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
* SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
* INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
* CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
* ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
* POSSIBILITY OF SUCH DAMAGE.
*/
package cloud.piranha.feature.logging;

import java.util.logging.Handler;
import java.util.logging.LogRecord;
import java.util.logging.ConsoleHandler;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.Future;
import java.util.concurrent.TimeoutException;
import java.util.concurrent.TimeUnit;

public class LoggingHandler extends Handler {

/**
* Stores the executor service for handling log records.
*/
private final ExecutorService executorService = Executors.newSingleThreadExecutor();

/**
* Stores the original ConsoleHandler.
*/
private final ConsoleHandler consoleHandler = new ConsoleHandler();

/**
* Constructor.
*/
public LoggingHandler() {
consoleHandler.setLevel(getLevel());
}

@Override
public void publish(LogRecord record) {
executorService.submit(new LogRecordPublisher(record));
}

/**
* The LogRecordPublisher class.
*/
private class LogRecordPublisher implements Runnable {

/**
* Stores the LogRecord.
*/
private final LogRecord record;

/**
* Constructor.
*
* @param record the log record.
*/
LogRecordPublisher(LogRecord record) {
this.record = record;
}

@Override
public void run() {
Future<?> future = executorService.submit(() -> consoleHandler.publish(record));
try {
future.get(10, TimeUnit.SECONDS);
} catch (TimeoutException e) {
future.cancel(true);
} catch (Exception e) {
// Handle other exceptions
}
}
}

@Override
public void flush() {
consoleHandler.flush();
}

@Override
public void close() throws SecurityException {
consoleHandler.close();
executorService.shutdown();
}
}
2 changes: 1 addition & 1 deletion feature/logging/src/main/java/module-info.java
Original file line number Diff line number Diff line change
Expand Up @@ -36,5 +36,5 @@
exports cloud.piranha.feature.logging;
opens cloud.piranha.feature.logging;
requires transitive cloud.piranha.feature.impl;
requires java.logging;
requires transitive java.logging;
}
Original file line number Diff line number Diff line change
Expand Up @@ -27,9 +27,10 @@
*/
package cloud.piranha.feature.logging;

import static java.util.logging.Level.INFO;
import static java.util.logging.Level.ALL;
import static java.util.logging.Level.SEVERE;
import java.util.logging.LogManager;

import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertNull;
import org.junit.jupiter.api.Test;
Expand Down Expand Up @@ -59,7 +60,7 @@ void testGetLevel() {
void testInit() {
LoggingFeature feature = new LoggingFeature();
feature.init();
assertEquals(INFO, LogManager.getLogManager().getLogger("").getLevel());
assertEquals(ALL, LogManager.getLogManager().getLogger("").getLevel());
feature = new LoggingFeature();
feature.setLevel("SEVERE");
feature.init();
Expand Down

0 comments on commit 731ce16

Please sign in to comment.