Simple-logger is a Java library for writing log files.
Configuration file in not required as all parameters can be set directly from
the code. Simple-logger provides standard logging levels:
TRACE, DEBUG, INFO, WARN, ERROR and FATAL. Simple-logger supports
file-rolling policy: max file size and number of backup files are configurable.
Simple-logger is written in Java and can run on all
systems with JRE 1.4 or above installed.
Quick start
Start using simple-logger by simply creating new SimpleLogger object:
SimpleLogger logger = new SimpleLogger();
At least path to log file should be set, otherwise simple-logger will write into
default log file (called simple-logger.log inside working directory):
logger.setFilename("/path/to/log/custom-name.log");
Now you are ready to log the first line:
logger.info("Hello simple-logger!");
Logging levels
The methods for writing to log files are similar to other
logger implementations, so the implementation library can easily be switched to
simple-logger.
simple-logger supports six levels of logging:
trace(String s)
debug(String s)
info(String s)
warn(String s)
error(String s)
fatal(String s)
All above methods for logging also accept additional argument 'Throwable',
which will write the whole exception stack trace to log file:
logger.error(String s, Throwable t);
Configuration
Logging parameters can be set inline in Java code and configuration changes
take effect immediately in runtime.
simple-logger offers the following methods for configuration:
Set path and name of log file. Eg: /path/to/log/file/mylog.log
Default value is ./simple-logger.log
setFilename(String filename)
Set true to append text to existing log file. If set to false,
log file will be overwritten and started from the scratch.
Default value is true.
setAppend(boolean append)
Set logging level. Logging levels are defined in LEVEL class:
TRACE, DEBUG, INFO, WARN, ERROR and FATAL
Default value is LEVEL.INFO.
setLogLevel(int level)
Set java specific date format. Eg. format yyyy.MM.dd hh:mm:ss:SSS
gives year, month, day, hour, minutes, seconds and milliseconds.
Default value is yyyy.MM.dd hh:mm:ss:SSS
setDateFormat(String format)
Set maximum size of log file in megabytes. When the limit is reached
new log file is opened and logging continues. Previous log file gets
.1 suffix.
Default value is 10.
setMaxSizeMb(int maxSizeMb)
Set number of rolling log files. When max file size of active log file
is reached, it is renamed to filename.1, and filename.1 is renamed to
filename.2 an so on... The number of last backup log files is defined
with backup parameter.
Default value is 5.
setBackup(int backup)
Enable 'System.out.println' option.
Default value false (disabled).
setVerbose(boolean verbose)
Set posix file permissions (Linux).
Default value is rw-r--r--.
setFilePermissions(String permissions)
Alternatively configure simple-logger with properties file. Here are the parameters:
simplelogger.filename=./simple-logger.log
simplelogger.level=INFO
simplelogger.append=true
simplelogger.verbose=false
simplelogger.maxFileSize=1
simplelogger.maxBackupFiles=2
simplelogger.filepermissions=rw-r--r--
simplelogger.dateFormat=yyyy.MM.dd hh:mm:ss:SSS
Another alternative is to configure simple-logger with environment variables:
SIMPLELOGGER_FILENAME="./simple-logger.log"
SIMPLELOGGER_LEVEL="INFO"
SIMPLELOGGER_APPEND="true"
SIMPLELOGGER_VERBOSE="true"
SIMPLELOGGER_MAXFILESIZE="1"
SIMPLELOGGER_MAXBACKUPFILES="2"
SIMPLELOGGER_FILEPERMISSIONS="rw-r--r--"
SIMPLELOGGER_DATEFORMAT="yyyy.MM.dd hh:mm:ss:SSS"
Note: Environment variables are always loaded first (if they exist, otherwise default values will apply). They can be overwritten by properties file or inline in the code whenever necessary.
Simple-logger is open source project and it is licenced under MIT license. Simple-logger is free to use, copy, modify and distribute.