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 call close().
  • Static mode: call the static overloads directly without instantiation. Each call opens and closes the file automatically.

Instance usage

Constructor

Description:

ConstructorDescription

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)

Description:

MethodReturn valuesDescription

info(String message, Object... params)

void

Writes an INFO-level entry to the log file. Supports %s / {0} style placeholders.

warn (instance)

Description:

MethodReturn valuesDescription

warn(String message, Object... params)

void

Writes a WARN-level entry to the log file.

error (instance)

Description:

MethodReturn valuesDescription

error(String message, Object... params)

void

Writes an ERROR-level entry to the log file.

close

Description:

MethodReturn valuesDescription

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

info (static, without Logger)

Description:

MethodReturn valuesDescription

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 %s and {0} style placeholders.

params: Optional substitution values.

info (static, with Logger)

Description:

MethodReturn valuesDescription

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)

Description:

MethodReturn valuesDescription

warn(String baseName, String message, Object... params)

void

Writes a WARN-level entry to the log file identified by baseName.

warn (static, with Logger)

Description:

MethodReturn valuesDescription

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)

Description:

MethodReturn valuesDescription

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)

Description:

MethodReturn valuesDescription

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)

Description:

MethodReturn valuesDescription

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)

Description:

MethodReturn valuesDescription

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);
        }
    }
}