You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

389 lines
11 KiB

11 years ago
package org.telegram.services;
import org.telegram.BuildVars;
import javax.validation.constraints.NotNull;
import java.io.*;
11 years ago
import java.util.Calendar;
import java.util.GregorianCalendar;
11 years ago
import java.util.concurrent.ConcurrentHashMap;
11 years ago
import java.util.concurrent.ConcurrentLinkedQueue;
import java.util.logging.ConsoleHandler;
11 years ago
import java.util.logging.Level;
import java.util.logging.Logger;
/**
* @author Ruben Bermudez
11 years ago
* @version 1.0
* @brief Logger
11 years ago
* @date 21/01/15
*/
public class BotLogger {
11 years ago
private volatile Object lockToWrite = new Object();
private final Logger logger;
11 years ago
private static volatile PrintWriter logginFile;
11 years ago
private Calendar lastFileDate;
11 years ago
private static volatile String currentFileName;
11 years ago
private static LoggerThread loggerThread = new LoggerThread();
11 years ago
private static volatile ConcurrentHashMap<String, BotLogger> instances = new ConcurrentHashMap<>();
11 years ago
private final static ConcurrentLinkedQueue<String> logsToFile = new ConcurrentLinkedQueue<>();
11 years ago
11 years ago
static {
11 years ago
11 years ago
loggerThread.start();
11 years ago
}
public static BotLogger getLogger(@NotNull String className) {
11 years ago
if (!instances.containsKey(className)) {
11 years ago
synchronized (BotLogger.class) {
11 years ago
if (!instances.containsKey(className)) {
11 years ago
BotLogger instance = new BotLogger(className);
instances.put(className, instance);
11 years ago
return instance;
} else {
return instances.get(className);
11 years ago
}
}
11 years ago
} else {
return instances.get(className);
11 years ago
}
11 years ago
}
11 years ago
11 years ago
private BotLogger(String classname) {
logger = Logger.getLogger(classname);
logger.setLevel(Level.ALL);
11 years ago
ConsoleHandler handler = new ConsoleHandler();
handler.setLevel(Level.ALL);
11 years ago
logger.addHandler(handler);
lastFileDate = new GregorianCalendar();
if (currentFileName == null || currentFileName.length() == 0) {
currentFileName = BuildVars.pathToLogs + dateFormaterForFileName(lastFileDate) + ".log";
try {
File file = new File(currentFileName);
if (!file.exists()) {
file.createNewFile();
}
logginFile = new PrintWriter(new BufferedWriter(new FileWriter(currentFileName, true)));
} catch (IOException e) {
e.printStackTrace();
}
}
11 years ago
}
11 years ago
11 years ago
public void log(@NotNull Level level, String msg) {
11 years ago
logger.log(level, msg);
11 years ago
logToFile(level, msg);
}
public void severe(String msg) {
11 years ago
logger.severe(msg);
11 years ago
logToFile(Level.SEVERE, msg);
}
public void warn(String msg) {
warning(msg);
}
public void debug(String msg) {
fine(msg);
}
public void error(String msg) {
severe(msg);
}
public void trace(String msg) {
finer(msg);
}
public void warning(String msg) {
11 years ago
logger.warning(msg);
11 years ago
logToFile(Level.WARNING, msg);
}
public void info(String msg) {
11 years ago
logger.info(msg);
11 years ago
logToFile(Level.INFO, msg);
}
public void config(String msg) {
11 years ago
logger.config(msg);
11 years ago
logToFile(Level.CONFIG, msg);
}
public void fine(String msg) {
11 years ago
logger.fine(msg);
11 years ago
logToFile(Level.FINE, msg);
}
public void finer(String msg) {
11 years ago
logger.finer(msg);
11 years ago
logToFile(Level.FINER, msg);
}
public void finest(String msg) {
11 years ago
logger.finest(msg);
11 years ago
logToFile(Level.FINEST, msg);
}
public void log(@NotNull Level level, @NotNull Throwable throwable) {
11 years ago
throwable.printStackTrace();
11 years ago
logToFile(level, throwable);
}
public void log(@NotNull Level level, String msg, Throwable thrown) {
11 years ago
logger.log(level, msg, thrown);
logToFile(level, msg ,thrown);
11 years ago
}
public void severe(@NotNull Throwable throwable) {
logToFile(Level.SEVERE, throwable);
}
public void warning(@NotNull Throwable throwable) {
logToFile(Level.WARNING, throwable);
}
public void info(@NotNull Throwable throwable) {
logToFile(Level.INFO, throwable);
}
public void config(@NotNull Throwable throwable) {
logToFile(Level.CONFIG, throwable);
}
public void fine(@NotNull Throwable throwable) {
logToFile(Level.FINE, throwable);
}
public void finer(@NotNull Throwable throwable) {
logToFile(Level.FINER, throwable);
}
public void finest(@NotNull Throwable throwable) {
logToFile(Level.FINEST, throwable);
}
public void warn(Throwable throwable) {
warning(throwable);
}
public void debug(Throwable throwable) {
fine(throwable);
}
public void error(Throwable throwable) {
severe(throwable);
}
public void trace(Throwable throwable) {
finer(throwable);
}
public void severe(String msg, @NotNull Throwable throwable) {
log(Level.SEVERE, msg, throwable);
}
public void warning(String msg, @NotNull Throwable throwable) {
log(Level.WARNING, msg, throwable);
}
public void info(String msg, @NotNull Throwable throwable) {
log(Level.INFO, msg, throwable);
}
public void config(String msg, @NotNull Throwable throwable) {
log(Level.CONFIG, msg, throwable);
}
public void fine(String msg, @NotNull Throwable throwable) {
log(Level.FINE, msg, throwable);
}
public void finer(String msg, @NotNull Throwable throwable) {
log(Level.FINER, msg, throwable);
}
public void finest(String msg, @NotNull Throwable throwable) {
log(Level.FINEST, msg, throwable);
}
public void warn(String msg, @NotNull Throwable throwable) {
log(Level.WARNING, msg, throwable);
}
public void debug(String msg, @NotNull Throwable throwable) {
log(Level.FINE, msg, throwable);
}
public void error(String msg, @NotNull Throwable throwable) {
log(Level.SEVERE, msg, throwable);
}
public void trace(String msg, @NotNull Throwable throwable) {
log(Level.FINER, msg, throwable);
}
11 years ago
private boolean isCurrentDate(Calendar calendar) {
if (calendar.get(Calendar.DAY_OF_MONTH) != lastFileDate.get(Calendar.DAY_OF_MONTH)) {
return false;
}
if (calendar.get(Calendar.MONTH) != lastFileDate.get(Calendar.MONTH)) {
return false;
}
if (calendar.get(Calendar.YEAR) != lastFileDate.get(Calendar.YEAR)) {
return false;
}
return true;
11 years ago
}
11 years ago
private String dateFormaterForFileName(@NotNull Calendar calendar) {
11 years ago
String dateString = "";
11 years ago
dateString += calendar.get(Calendar.DAY_OF_MONTH);
dateString += calendar.get(Calendar.MONTH) + 1;
dateString += calendar.get(Calendar.YEAR);
11 years ago
return dateString;
}
11 years ago
private String dateFormaterForLogs(@NotNull Calendar calendar) {
11 years ago
String dateString = "[";
11 years ago
dateString += calendar.get(Calendar.DAY_OF_MONTH) + "_";
dateString += (calendar.get(Calendar.MONTH) + 1) + "_";
dateString += calendar.get(Calendar.YEAR) + "_";
dateString += calendar.get(Calendar.HOUR_OF_DAY) + "_";
dateString += calendar.get(Calendar.MINUTE) + ":";
dateString += calendar.get(Calendar.SECOND);
11 years ago
dateString += "] ";
return dateString;
}
11 years ago
private void updateAndCreateFile(Calendar calendar) {
if (isCurrentDate(calendar)) {
return;
}
lastFileDate = new GregorianCalendar();
currentFileName = BuildVars.pathToLogs + dateFormaterForFileName(lastFileDate) + ".log";
try {
logginFile.flush();
logginFile.close();
File file = new File(currentFileName);
if (!file.exists()) {
file.createNewFile();
11 years ago
}
11 years ago
logginFile = new PrintWriter(new BufferedWriter(new FileWriter(currentFileName, true)));
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
11 years ago
}
}
private void logToFile(@NotNull Level level, Throwable throwable) {
11 years ago
if (!isLoggable(level)){
return;
}
synchronized (lockToWrite) {
Calendar currentDate = new GregorianCalendar();
String dateForLog = dateFormaterForLogs(currentDate);
updateAndCreateFile(currentDate);
logThrowableToFile(level, throwable, dateForLog);
11 years ago
}
}
11 years ago
11 years ago
private void logToFile(@NotNull Level level, String msg) {
11 years ago
if (!isLoggable(level)){
return;
}
synchronized (lockToWrite) {
Calendar currentDate = new GregorianCalendar();
updateAndCreateFile(currentDate);
String dateForLog = dateFormaterForLogs(currentDate);
logMsgToFile(level, msg, dateForLog);
11 years ago
}
}
private void logToFile(Level level, String msg, Throwable throwable) {
11 years ago
if (!isLoggable(level)){
11 years ago
return;
}
synchronized (lockToWrite) {
11 years ago
Calendar currentDate = new GregorianCalendar();
11 years ago
updateAndCreateFile(currentDate);
11 years ago
String dateForLog = dateFormaterForLogs(currentDate);
11 years ago
logMsgToFile(level, msg, dateForLog);
logThrowableToFile(level, throwable, dateForLog);
}
}
private void logMsgToFile(Level level, String msg, String dateForLog) {
11 years ago
dateForLog += " [" + logger.getName() + "]" + level.toString() + " - " + msg;
11 years ago
logsToFile.add(dateForLog);
synchronized (logsToFile) {
logsToFile.notifyAll();
}
11 years ago
}
private void logThrowableToFile(Level level, Throwable throwable, String dateForLog) {
11 years ago
String throwableLog = dateForLog + level.getName() + " - " + throwable + "\n";
11 years ago
for (StackTraceElement element : throwable.getStackTrace()) {
11 years ago
throwableLog += "\tat " + element + "\n";
}
logsToFile.add(throwableLog);
synchronized (logsToFile) {
logsToFile.notifyAll();
11 years ago
}
}
private boolean isLoggable(Level level) {
11 years ago
return logger.isLoggable(level) && BuildVars.debug;
11 years ago
}
@Override
protected void finalize() throws Throwable {
logginFile.flush();
logginFile.close();
super.finalize();
}
11 years ago
private static class LoggerThread extends Thread {
@Override
public void run() {
setPriority(Thread.MIN_PRIORITY);
while(true) {
ConcurrentLinkedQueue<String> stringsToLog = new ConcurrentLinkedQueue<>();
synchronized (logsToFile) {
if (logsToFile.isEmpty()) {
try {
logsToFile.wait();
} catch (InterruptedException e) {
return;
}
if (logsToFile.isEmpty()) {
continue;
}
}
stringsToLog.addAll(logsToFile);
logsToFile.clear();
}
for (String stringToLog: stringsToLog) {
logginFile.println(stringToLog);
}
logginFile.flush();
}
}
}
11 years ago
}