Skip to content

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.

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.

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.

Description:

Method Return values Description
warn(String message, Object… params) void Writes a WARN-level entry to the log file.

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

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 %s and {0} style placeholders.
  • params: Optional substitution values.

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

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.

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

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.

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.

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.

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