A Log singleton in java
I needed a simple logger in java. I wrote a quick singleton. I sort of want to keep this around if I ever need to use it again, so I thought I would post it here.
Note: Feel free to complain about this singleton using my own code style and not the java code style. 🙂
package ClinicSoft.Singletons; import java.io.File; import java.io.FileWriter; public class Log { public static Log Instance = new Log(); private String _FileName; private String _Extension = ".log"; private int _LogId = -1; private Log() { SetFileName("ClinicSoft"); } public String GetFileName() { if (_LogId > -1) return _FileName + "." + _LogId + _Extension; else return _FileName + _Extension; } // The root file name, without the extension public void SetFileName(String inFileName) { _FileName = inFileName; while (FileExists(GetFileName())) _LogId++; } private boolean FileExists(String inFile) { return new File(inFile).exists(); } public static void WriteLine(String inLogMessage) { Log.Instance.WriteLog(inLogMessage + System.getProperty("line.separator")); } public void WriteLog(String inLogMessage) { try { // Create file FileWriter file = new FileWriter(GetFileName(), true); file.write(inLogMessage); // Close the output stream file.close(); } catch (Exception e) { // Catch exception if any System.err.println("Error: " + e.getMessage()); } } }