In this blog post, don’t get me wrong, I think Log4J is a great product and I am not trying to discourage anyone from using it.
I have used Log4J v1.x on hundreds of projects for customers and myself. It has always worked exactly as expected. I have never used Log4J v2.x, so I cannot/will not comment on the current security vulnerability that has been discovered.
All of the logging I have configured for use with Log4J has been extremely basic. I use the basic appender and have it write to a file. That’s it. Basically, I am using probably 5% of the available features. So, over time, I have been slowing moving away from Log4J and just using my own simple logger because that is all I have ever needed.
So, this week I spent some time prettying up my SimpleLogger code. It was written in a similar style to Log4J but there is no configuration file for it. The code is thread-safe and can be used in any project that requires simple logging code.
- public static void setLogFile(String logFileName)
- public static void setLogFile(String logFileName, String logDirectory)
- public static void close()
- public static void setIncludePackageName(boolean flag)
- public static void setLevel(LogLevel ll)
- public static void setMaxBackupFiles(int maxCount)
- public static void setMaxFileSize(int maxSize)
- public static void setRotationType(LogRotationType rt)
- public static void error(Object data)
- public static void warn(Object data)
- public static void info(Object data)
- public static void debug(Object data)
- public static void errorDump(String title, byte[] data)
- public static void warnDump(String title, byte[] data)
- public static void infoDump(String title, byte[] data)
- public static void debugDump(String title, byte[] data)
Here is a simple tester Java program that shows how to use the various methods:
package com.capitalware.logging; import com.capitalware.logging.SimpleLogger.*; /** * This class will test the SimpleLogger class. * * @author Roger Lacroix, Capitalware Inc. * @version 1.0.0 * @license Apache 2 License */ public class Test_Logger { public Test_Logger() { SimpleLogger.setLogFile("Test_Logger.log", "C:\\temp\\"); SimpleLogger.setLevel(LogLevel.DEBUG); SimpleLogger.setRotationType(LogRotationType.SIZE); SimpleLogger.setIncludePackageName(false); SimpleLogger.error("this is a test message for error."); SimpleLogger.warn("this is a test message for warn."); SimpleLogger.info("this is a test message for info."); SimpleLogger.debug("this is a test message for debug."); SimpleLogger.debugDump("Kids song", "Mary had a little lamb, Little lamb, little lamb, Mary had a little lamb Whose fleece was white as snow.".getBytes()); SimpleLogger.close(); } public static void main(String[] args) { new Test_Logger(); } }
Here’s what the output looks like in the log file:
2021/12/15 20:27:58.187 ERROR (Test_Logger.<init>) this is a test message for error. 2021/12/15 20:27:58.188 WARN (Test_Logger.<init>) this is a test message for warn. 2021/12/15 20:27:58.188 INFO (Test_Logger.<init>) this is a test message for info. 2021/12/15 20:27:58.188 DEBUG (Test_Logger.<init>) this is a test message for debug. 2021/12/15 20:27:58.188 DEBUG (Test_Logger.<init>) Kids song -->> 2021/12/15 20:27:58.188 DEBUG (Test_Logger.<init>) 000000: 4D617279 20686164 2061206C 6974746C 65206C61 6D622C20 4C697474 6C65206C Mary had a little lamb, Little l 2021/12/15 20:27:58.188 DEBUG (Test_Logger.<init>) 000020: 616D622C 206C6974 746C6520 6C616D62 2C204D61 72792068 61642061 206C6974 amb, little lamb, Mary had a lit 2021/12/15 20:27:58.188 DEBUG (Test_Logger.<init>) 000040: 746C6520 6C616D62 2057686F 73652066 6C656563 65207761 73207768 69746520 tle lamb Whose fleece was white 2021/12/15 20:27:58.188 DEBUG (Test_Logger.<init>) 000060: 61732073 6E6F772E as snow. 2021/12/15 20:27:58.188 DEBUG (Test_Logger.<init>) <<-----
I have generated the JavaDocs for the SimpleLogger class and created a JAR file for SimpleLogger for users to use. I zipped up the JavaDocs, JAR file and the source code for both Test_SimpleLogger.java program and SimpleLogger.java. You can download the it from here.
Simply add the SimpleLogger JAR file to your application or add the SimpleLogger source code to your utility (or handler) package of your application and then start using it.
Regards,
Roger Lacroix
Capitalware Inc.