Skip to content

Instantly share code, notes, and snippets.

@alksily
Created March 1, 2019 12:25
Show Gist options
  • Save alksily/447944553c70912e55b4ec1076c833dd to your computer and use it in GitHub Desktop.
Save alksily/447944553c70912e55b4ec1076c833dd to your computer and use it in GitHub Desktop.
Logger.java
package ru.aengine.tgm.util;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.OutputStreamWriter;
import java.io.UnsupportedEncodingException;
/**
* Logger class for logging data in system console or/and console.log file
*/
public class Logger{
private static boolean consoleLogging = true;
private static boolean fileLogging = false;
private static BufferedWriter consoleOut;
private static BufferedWriter fileOut;
/**
* Logger class for logging data in system console or/and console.log file
*/
public Logger(){
init();
}
/**
* Logger class for logging data in system console or/and console.log file
*
* @param fileLog
* boolean - turn on/off logging in file
*/
public Logger(boolean fileLog){
fileLogging = fileLog;
init();
}
/**
* Logger class for logging data in system console or/and console.log file
*
* @param consoleLog
* boolean - turn on/off logging in console
* @param fileLog
* boolean - turn on/off logging in file
*/
public Logger(boolean consoleLog, boolean fileLog){
consoleLogging = consoleLog;
fileLogging = fileLog;
init();
}
private void init(){
System.setProperty("file.encoding", "cp866");
if(consoleLogging){
try{
consoleOut = new BufferedWriter(new OutputStreamWriter(System.out, System.getProperty("file.encoding")));
}catch(UnsupportedEncodingException e){
e.printStackTrace();
}
}
if(fileLogging){
try{
File log = new File("console.log");
if(log.exists()) log.delete();
log.createNewFile();
fileOut = new BufferedWriter(new OutputStreamWriter(new FileOutputStream(log), System.getProperty("file.encoding")));
}catch(IOException e){
e.printStackTrace();
}
}
}
/**
* Write line without new line in end
*
* @param msg
* all types of data
*/
public static void print(Object msg){
if(consoleLogging) consoleWrite(msg.toString(), false);
if(fileLogging) fileWrite(msg.toString(), false);
}
/**
* Write line with new line in end
*
* @param msg
* all types of data
*/
public static void println(Object msg){
if(consoleLogging) consoleWrite(msg.toString(), true);
if(fileLogging) fileWrite(msg.toString(), true);
}
/**
* Write empty line with new line in end
*/
public static void println(){
if(consoleLogging) consoleWrite(null, true);
if(fileLogging) fileWrite(null, true);
}
private static void consoleWrite(String msg, boolean ln){
try{
if(msg != null) consoleOut.write(msg);
if(ln) consoleOut.newLine();
consoleOut.flush();
}catch(IOException e){
e.printStackTrace();
}
}
private static void fileWrite(String msg, boolean ln){
try{
if(msg != null) fileOut.write(msg);
if(ln) fileOut.newLine();
fileOut.flush();
}catch(IOException e){
e.printStackTrace();
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment