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
Constructor
Description:
| Constructor | Description |
|---|---|
|
FileLogger(String baseName) |
Creates a new |
|
baseName: The base name of the log file, without date suffix or extension. |
|
info (instance)
Description:
| Method | Return values | Description |
|---|---|---|
|
info(String message, Object... params) |
void |
Writes an INFO-level entry to the log file. Supports |
warn (instance)
Description:
| Method | Return values | Description |
|---|---|---|
|
warn(String message, Object... params) |
void |
Writes a WARN-level entry to the log file. |
error (instance)
Description:
| Method | Return values | Description |
|---|---|---|
|
error(String message, Object... params) |
void |
Writes an ERROR-level entry to the log file. |
close
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
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: The base name of the log file. message: The message text. Supports params: Optional substitution values. |
||
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)
Description:
| Method | Return values | Description |
|---|---|---|
|
warn(String baseName, String message, Object... params) |
void |
Writes a WARN-level entry to the log file identified by |
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)
Description:
| Method | Return values | Description |
|---|---|---|
|
error(String baseName, String message, Object... params) |
void |
Writes an ERROR-level entry to the log file identified by |
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)
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 |
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);
}
}
}