Java tutorial
/* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * Copyright (C) 2015, Markus Staudt <info@braffdev.com> * * * * This program is free software: you can redistribute it and/or modify * * it under the terms of the GNU General Public License as published by * * the Free Software Foundation, either version 3 of the License, or * * (at your option) any later version. * * * * This program is distributed in the hope that it will be useful, * * but WITHOUT ANY WARRANTY; without even the implied warranty of * * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * * GNU General Public License for more details. * * * * You should have received a copy of the GNU General Public License * * along with this program. If not, see <http://www.gnu.org/licenses/>. * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */ package com.braffdev.server.core.io.logger; import java.text.SimpleDateFormat; import java.util.Date; import java.util.Locale; import org.apache.commons.lang3.exception.ExceptionUtils; import com.braffdev.server.Server; import com.braffdev.server.core.config.types.server.ServerConfig; /** * */ public abstract class Logger { /** * Constructs a new logger with the name of the given class. * * @param clazz the class. * @return the constructed logger. */ public static Logger getLogger(Class<?> clazz) { return Logger.getLogger(clazz.getSimpleName()); } /** * Constructs a new logger with the given tag. * * @param tag the tag. * @return the constructed logger. */ public static Logger getLogger(String tag) { return new HybridLogger(tag); } private static boolean logDebug; private String tag; private String format; /** * Constructs a new logger with the name of the given class. * * @param clazz the class. * @return the constructed logger. */ protected Logger(Class<?> clazz) { this(clazz.getSimpleName()); } /** * Constructs a new logger with the given tag. * * @param tag the tag. * @return the constructed logger. */ protected Logger(String tag) { this.tag = tag; } /** * Logs the given message with the <code>INFO</code> tag. * * @param msg */ public void info(String msg) { this.buildLogMessage("INFO", msg); } /** * Logs the given message with the <code>DEBUG</code> tag.<br> * This log will only be printed if debug-level logging is enabled. * * @param msg */ public void debug(String msg) { if (Logger.isLogDebug()) { this.buildLogMessage("DEBUG", msg); } } /** * Logs the given Throwable with the <code>DEBUG</code> tag.<br> * This log will only be printed if debug-level logging is enabled. * * @param t */ public void debug(Throwable t) { if (Logger.isLogDebug()) { this.buildLogMessage("DEBUG", ExceptionUtils.getStackTrace(t)); } } /** * Logs the given message with the <code>WARN</code> tag. * * @param msg */ public void warning(String msg) { this.buildLogMessage("WARN", msg); } /** * Logs the given message with the <code>ERROR</code> tag. * * @param msg */ public void error(String msg) { this.buildLogMessage("ERROR", msg); } /** * Logs the given Throwable with the <code>ERROR</code> tag. * * @param t */ public void error(Throwable t) { this.buildLogMessage("ERROR", ExceptionUtils.getStackTrace(t)); } /** * Logs the given message with the <code>ERROR</code> tag and appends the given Throwable as "cause" section. * * @param msg * @param t */ public void error(String msg, Throwable t) { this.error(msg, ExceptionUtils.getStackTrace(t)); } /** * Logs the given message with the <code>ERROR</code> tag and appends the given cause as "cause" section. * * @param msg * @param t */ public void error(String msg, String cause) { this.error(msg + " | Cause: " + cause); } /** * Logs the given Throwable with the <code>CRITICAL</code> tag. * * @param msg */ public void critical(String msg) { this.buildLogMessage("CRITICAL", msg); } /** * Logs the given Throwable with the <code>CRITICAL</code> tag. * * @param t */ public void critical(Throwable t) { this.buildLogMessage("CRITICAL", ExceptionUtils.getStackTrace(t)); } /** * Logs the given message with the <code>CRITICAL</code> tag and appends the given Throwable as "cause" section. * * @param msg * @param t */ public void critical(String msg, Throwable t) { this.buildLogMessage("CRITICAL", msg + " | Cause: " + ExceptionUtils.getStackTrace(t)); } /** * @param out * @param msg */ protected synchronized void buildLogMessage(String code, String msg) { StringBuilder builder = new StringBuilder(); if (msg != null) { msg = msg.replace("\n", "\n "); } else { msg = ""; } String[] msgLines = msg.split("\n"); for (int i = 0; i < msgLines.length; i++) { String line = msgLines[i]; builder.append(this.getDate()); builder.append(" "); builder.append(code); builder.append(" ["); builder.append(this.tag); builder.append("] "); builder.append(line); if (((i + 1) < msgLines.length) && !line.endsWith("\n")) { builder.append("\r\n"); } } this.log(builder.toString()); } /** * @param msg */ protected abstract void log(String msg); /** * @return */ private String getDate() { String format = this.getFormat(); if (format == null) { return null; } SimpleDateFormat sdf = null; try { sdf = new SimpleDateFormat(format, Locale.ENGLISH); } catch (IllegalArgumentException e) { // illegal format - use fallback sdf = new SimpleDateFormat("", Locale.ENGLISH); } return sdf.format(new Date()); } /** * @return */ private String getFormat() { if (this.format == null) { ServerConfig config = Server.getConfig(); if (config != null) { this.format = config.getLog().getFormat(); } } return this.format; } /** * @return whether debug-level logging is enabled */ public static boolean isLogDebug() { return Logger.logDebug; } /** * Enables or disables debug-level logging */ public static void setLogDebug(boolean logDebug) { Logger.logDebug = logDebug; } }