FileLogger
Utility class for writing timestamped log entries to date-stamped files in the OpenKM log directory. Log files are named <baseName>_yyyyMMdd.log and are created or appended in the Config.LOG_DIR folder.
FileLogger supports two usage modes:
- Instance mode: construct a
FileLogger, call instance methods, then callclose(). - Static mode: call the static overloads directly without instantiation. Each call opens and closes the file automatically.
Instance usage
Section titled “Instance usage”Constructor
Section titled “Constructor”Description:
| Constructor | Description |
|---|---|
| FileLogger(String baseName) | Creates a new FileLogger that appends to the file <LOG_DIR>/<baseName>_yyyyMMdd.log. |
- baseName: The base name of the log file, without date suffix or extension.
info (instance)
Section titled “info (instance)”Description:
| Method | Return values | Description |
|---|---|---|
| info(String message, Object… params) | void | Writes an INFO-level entry to the log file. Supports %s / {0} style placeholders. |
warn (instance)
Section titled “warn (instance)”Description:
| Method | Return values | Description |
|---|---|---|
| warn(String message, Object… params) | void | Writes a WARN-level entry to the log file. |
error (instance)
Section titled “error (instance)”Description:
| Method | Return values | Description |
|---|---|---|
| error(String message, Object… params) | void | Writes an ERROR-level entry to the log file. |
Description:
| Method | Return values | Description |
|---|---|---|
| close() | void | Closes the underlying file writer. Must be called when the instance is no longer needed. |
Example (instance mode):
package com.openkm;
import com.openkm.util.FileLogger;
public class Test {
public static void main(String[] args) { FileLogger logger = new FileLogger("myProcess"); try { logger.info("Starting process for user %s", "admin"); logger.warn("Retrying step %s", "step1"); logger.error("Failed with code %s", "500"); } finally { logger.close(); } }}Static usage
Section titled “Static usage”info (static, without Logger)
Section titled “info (static, without Logger)”Description:
| Method | Return values | Description |
|---|---|---|
| info(String baseName, String message, Object… params) | void | Writes an INFO-level entry to the log file identified by baseName. |
- baseName: The base name of the log file.
- message: The message text. Supports
%sand{0}style placeholders. - params: Optional substitution values.
info (static, with Logger)
Section titled “info (static, with Logger)”Description:
| Method | Return values | Description |
|---|---|---|
| info(Logger logger, String baseName, String message, Object… params) | void | Writes an INFO-level entry to both the file and the provided SLF4J logger. |
Example:
package com.openkm;
import com.openkm.util.FileLogger;
public class Test {
public static void main(String[] args) { try { FileLogger.info("logName", "This is info"); FileLogger.info("logName", "Parameters: %s %s", "one", "two"); } catch (Exception e) { e.printStackTrace(); } }}warn (static, without Logger)
Section titled “warn (static, without Logger)”Description:
| Method | Return values | Description |
|---|---|---|
| warn(String baseName, String message, Object… params) | void | Writes a WARN-level entry to the log file identified by baseName. |
warn (static, with Logger)
Section titled “warn (static, with Logger)”Description:
| Method | Return values | Description |
|---|---|---|
| warn(Logger logger, String baseName, String message, Object… params) | void | Writes a WARN-level entry to both the file and the provided SLF4J logger. |
Example:
package com.openkm;
import com.openkm.util.FileLogger;
public class Test {
public static void main(String[] args) { try { FileLogger.warn("logName", "This is warning"); FileLogger.warn("logName", "Parameters: %s %s", "one", "two"); } catch (Exception e) { e.printStackTrace(); } }}error (static, without Logger)
Section titled “error (static, without Logger)”Description:
| Method | Return values | Description |
|---|---|---|
| error(String baseName, String message, Object… params) | void | Writes an ERROR-level entry to the log file identified by baseName. |
error (static, with Logger and message)
Section titled “error (static, with Logger and message)”Description:
| Method | Return values | Description |
|---|---|---|
| error(Logger logger, String baseName, String message, Object… params) | void | Writes an ERROR-level entry to both the file and the provided SLF4J logger. |
error (static, with Logger and Throwable)
Section titled “error (static, with Logger and Throwable)”Description:
| Method | Return values | Description |
|---|---|---|
| error(Logger logger, String baseName, String message, Throwable e) | void | Writes an ERROR-level entry combining the provided message with the full stack trace of e to both the file and the SLF4J logger. |
error (static, Throwable only)
Section titled “error (static, Throwable only)”Description:
| Method | Return values | Description |
|---|---|---|
| error(Logger logger, String baseName, Throwable e) | void | Writes an ERROR-level entry using the exception message and full stack trace to both the file and the SLF4J logger. |
Example:
package com.openkm;
import com.openkm.util.FileLogger;import org.slf4j.Logger;import org.slf4j.LoggerFactory;
public class Test {
private static final Logger log = LoggerFactory.getLogger(Test.class);
public static void main(String[] args) { try { // Static without logger FileLogger.error("logName", "This is error"); FileLogger.error("logName", "Parameters: %s %s", "one", "two");
// Static with logger and throwable throw new RuntimeException("something failed"); } catch (Exception e) { FileLogger.error(log, "logName", "Unexpected error", e); } }}