Skip to content

Commit

Permalink
Changes:
Browse files Browse the repository at this point in the history
- Log-buffer for messages before logging initialization (fix)
- Correct encodings language files (again)
  • Loading branch information
autumoswitzerland committed Sep 29, 2024
1 parent 754a89a commit b0e9a7d
Show file tree
Hide file tree
Showing 9 changed files with 285 additions and 164 deletions.
30 changes: 19 additions & 11 deletions src/main/java/ch/autumo/beetroot/BeetRootConfigurationManager.java
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,9 @@

import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.nio.charset.StandardCharsets;
import java.util.ArrayList;
Expand All @@ -39,6 +41,8 @@
import org.slf4j.LoggerFactory;
import org.w3c.dom.Document;

import ch.autumo.beetroot.logging.LogBuffer;
import ch.autumo.beetroot.logging.LogBuffer.LogLevel;
import ch.autumo.beetroot.security.SecureApplication;
import ch.autumo.beetroot.utils.Helper;
import ch.autumo.beetroot.utils.security.Security;
Expand Down Expand Up @@ -204,8 +208,7 @@ public synchronized void initializeWithFullPath(String configFilePath) throws Ex
if (servletContext == null) {

if (rootPath == null || rootPath.length() == 0) {

LOG.error("Specified '-DROOTPATH' is non-existant! Check starting script of java process.");
LogBuffer.log(LogLevel.ERROR, "Specified '-DROOTPATH' is non-existant! Check starting script of java process.");
throw new Exception("Specified '-DROOTPATH' is non-existant! Check starting script of java process.");
}

Expand All @@ -215,8 +218,7 @@ public synchronized void initializeWithFullPath(String configFilePath) throws Ex

final File dir = new File(rootPath);
if (!dir.exists() || !dir.isDirectory()) {

LOG.error("Specified '-DROOTPATH' is invalid! Check starting script of java process.");
LogBuffer.log(LogLevel.ERROR, "Specified '-DROOTPATH' is invalid! Check starting script of java process.");
throw new Exception("Specified '-DROOTPATH' is non-existant! Check starting script of java process.");
}
}
Expand Down Expand Up @@ -247,7 +249,7 @@ public synchronized void initializeWithFullPath(String configFilePath) throws Ex
generalProps.load(BeetRootConfigurationManager.class.getResourceAsStream(file));

} catch (IOException e) {
LOG.error("Couldn't read general server configuration '" + file + "' !", e);
LogBuffer.log(LogLevel.ERROR, "Couldn't read general server configuration '{}' !", file, e);
throw new Exception("Couldn't read general server configuration '" + file + "' !");
} finally {
if (fis != null)
Expand All @@ -258,12 +260,12 @@ public synchronized void initializeWithFullPath(String configFilePath) throws Ex
// load some main props separately
this.csrf = getYesOrNo(Constants.KEY_WS_USE_CSRF_TOKENS, Constants.YES);
if (this.csrf)
LOG.info("CSRF activated!");
LogBuffer.log(LogLevel.INFO, "CSRF activated!");

this.extendedRoles = getYesOrNo(Constants.KEY_WS_USE_EXT_ROLES, Constants.YES);
this.translateTemplates = getYesOrNo(Constants.KEY_WEB_TRANSLATIONS, Constants.NO);
if (this.translateTemplates)
LOG.info("Web templates are translated.");
LogBuffer.log(LogLevel.INFO, "Web templates are translated.");


// HTML Input map
Expand All @@ -277,11 +279,14 @@ public synchronized void initializeWithFullPath(String configFilePath) throws Ex
fis = new FileInputStream(mapFile);
this.htmlInputMap.load(fis);
} else {
this.htmlInputMap.load(BeetRootConfigurationManager.class.getResourceAsStream(htmlMap));
final InputStream is = BeetRootConfigurationManager.class.getResourceAsStream(htmlMap);
this.htmlInputMap.load(is);
if (is == null)
throw new FileNotFoundException("The HTML input map file could not be loaded during the streaming attempt.");
}
} catch (IOException e) {
htmlInputMap = null;
LOG.error("Couldn't read additionl HTML input mapping file '" + htmlMap + "' !", e);
LogBuffer.log(LogLevel.ERROR, "Couldn't read additionl HTML input mapping file '{}' !", htmlMap, e);
throw new Exception("Couldn't read additionl HTML input mapping file '" + htmlMap + "' !");
} finally {
if (fis != null)
Expand All @@ -300,12 +305,15 @@ public synchronized void initializeWithFullPath(String configFilePath) throws Ex
isr = new InputStreamReader(new FileInputStream(f), StandardCharsets.UTF_8);
this.languageMap.load(isr);
} else {
isr = new InputStreamReader(BeetRootConfigurationManager.class.getResourceAsStream(file), StandardCharsets.UTF_8);
final InputStream is = BeetRootConfigurationManager.class.getResourceAsStream(file);
if (is == null)
throw new FileNotFoundException("Language file could not be loaded during the streaming attempt.");
isr = new InputStreamReader(is, StandardCharsets.UTF_8);
this.languageMap.load(isr);
}
} catch (IOException e) {
this.languageMap = null;
LOG.warn("Couldn't read languages file '" + file + "' !", e);
LogBuffer.log(LogLevel.WARN, "Couldn't read languages file '{}' !", file, e);
} finally {
if (isr != null)
isr.close();
Expand Down
107 changes: 107 additions & 0 deletions src/main/java/ch/autumo/beetroot/logging/LogBuffer.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,107 @@
/**
*
* Copyright (c) 2024 autumo Ltd. Switzerland, Michael Gasche
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*
*/
package ch.autumo.beetroot.logging;

import java.util.ArrayList;
import java.util.List;

import org.slf4j.Logger;


/**
* Log-buffer; used before the logging system is initialized.
*/
public class LogBuffer {

/** List to buffer logs. */
private static final List<LogEntry> logBuffer = new ArrayList<>();

/** Enum for logging severity levels. */
public enum LogLevel {
INFO, DEBUG, ERROR, WARN, TRACE
}

/** Inner class to hold log entry data (message, severity level). */
private static class LogEntry {
LogLevel level;
String message;
Object arguments[];
LogEntry(LogLevel level, String message) {
this.level = level;
this.message = message;
}
LogEntry(LogLevel level, String message, Object... arguments) {
this.level = level;
this.message = message;
this.arguments = arguments;
}
}

/**
* Log message to log buffer.
*
* @param level level
* @param message message
* @param arguments arguments
*/
public static void log(LogLevel level, String message) {
logBuffer.add(new LogEntry(level, message));
}

/**
* Log message to log buffer.
*
* @param level level
* @param message message
* @param arguments arguments
*/
public static void log(LogLevel level, String message, Object... arguments) {
logBuffer.add(new LogEntry(level, message, arguments));
}

/**
* Flush the buffered logs to the actual SLF4J logger
*
* @param logger the logger
*/
public static void flushToLogger(Logger logger) {
for (LogEntry entry : logBuffer) {
switch (entry.level) {
case INFO:
logger.info(entry.message, entry.arguments);
break;
case DEBUG:
logger.debug(entry.message, entry.arguments);
break;
case ERROR:
logger.error(entry.message, entry.arguments);
break;
case WARN:
logger.warn(entry.message, entry.arguments);
break;
case TRACE:
logger.trace(entry.message, entry.arguments);
break;
default:
logger.info(entry.message, entry.arguments);
}
}
logBuffer.clear(); // Clear buffer after flushing
}

}
6 changes: 6 additions & 0 deletions src/main/java/ch/autumo/beetroot/server/BaseServer.java
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@
import ch.autumo.beetroot.BeetRootDatabaseManager;
import ch.autumo.beetroot.BeetRootWebServer;
import ch.autumo.beetroot.Constants;
import ch.autumo.beetroot.logging.LogBuffer;
import ch.autumo.beetroot.logging.LogEventAppender;
import ch.autumo.beetroot.logging.LoggingFactory;
import ch.autumo.beetroot.server.action.Download;
Expand Down Expand Up @@ -262,6 +263,11 @@ public BaseServer(String params[]) {

//------------------------------------------------------------------------------

// Flush the messages that have been collected before log-system initialization
LogBuffer.flushToLogger(LOG);

//------------------------------------------------------------------------------

// DB manager initialization if not yet done!
try {

Expand Down
Loading

0 comments on commit b0e9a7d

Please sign in to comment.