001/******************************************************************************* 002The MIT License (MIT) 003 004Copyright (c) 2024 KILLCODING.COM 005 006Permission is hereby granted, free of charge, to any person obtaining a copy 007of this software and associated documentation files (the "Software"), to deal 008in the Software without restriction, including without limitation the rights 009to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 010copies of the Software, and to permit persons to whom the Software is 011furnished to do so, subject to the following conditions: 012 013The above copyright notice and this permission notice shall be included in 014all copies or substantial portions of the Software. 015 016THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 017IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 018FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 019AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 020LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 021OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN 022THE SOFTWARE. 023*****************************************************************************/ 024package com.killcoding.log; 025 026import java.util.Map; 027import java.io.File; 028import java.io.FilenameFilter; 029import java.util.Arrays; 030import java.util.List; 031import java.io.IOException; 032import java.util.HashMap; 033import java.util.Properties; 034import java.io.StringReader; 035import com.killcoding.tool.ConfigProperties; 036import com.killcoding.log.Logger; 037import com.killcoding.tool.FileTools; 038import java.net.URL; 039import com.killcoding.tool.CommonTools; 040import java.io.Reader; 041 042public final class LoggerFactory { 043 044 public final static String APP_LOGGER_KEY = "APP_LOGGER"; 045 private static boolean LOADED_CONFIG = false; 046 047 private static ConfigProperties defaultConfig = null; 048 049 static { 050 String hostname = CommonTools.getHostname(); 051 System.setProperty("HOSTNAME", hostname); 052 System.setProperty("hostname", hostname); 053 Logger.systemDebug(LoggerFactory.class, "===LoggerFactory Loading Config==="); 054 try { 055 File logCnfFile = CommonTools.getSystemProperties(LoggerFactory.class, APP_LOGGER_KEY, 056 "Logger.properties"); 057 if (logCnfFile == null) { 058 Logger.systemDebug(LoggerFactory.class, "Using default config."); 059 defaultConfig(); 060 } else { 061 String absPath = logCnfFile.getAbsolutePath(); 062 Logger.systemDebug(LoggerFactory.class, "Loading config(For System Init Debug) -> {}", absPath); 063 config(new File(absPath)); 064 } 065 Logger.systemDebug(LoggerFactory.class, "===LoggerFactory Loaded Config==="); 066 } catch (Exception e) { 067 Logger.systemDebug(LoggerFactory.class, "LoggerFactory static block falied!"); 068 Logger.systemError(LoggerFactory.class, e); 069 } 070 } 071 072 /** 073 * Use default config 074 * */ 075 public static synchronized void defaultConfig() { 076 defaultConfig = new ConfigProperties(); 077 try { 078 LogRecord.setConfig(defaultConfig); 079 LogRecord.start(); 080 } catch (Exception e) { 081 Logger.systemError(LoggerFactory.class, e); 082 } 083 } 084 085 /** 086 * Use target ConfigProperties 087 * @param _defaultConfig 088 * */ 089 public static synchronized void config(ConfigProperties _defaultConfig) { 090 defaultConfig = _defaultConfig; 091 try { 092 LogRecord.setConfig(defaultConfig); 093 LogRecord.start(); 094 } catch (Exception e) { 095 Logger.systemError(LoggerFactory.class, e); 096 } 097 LOADED_CONFIG = true; 098 } 099 100 /** 101 * Use target Logger.properties file 102 * @param loggerPropFile - It is file object 103 * @throws IOException 104 * */ 105 public static synchronized void config(File loggerPropFile) throws IOException { 106 if (!LOADED_CONFIG) { 107 defaultConfig = new ConfigProperties(); 108 Runnable updatedRun = new Runnable() { 109 @Override 110 public void run() { 111 try { 112 config(defaultConfig); 113 Logger.systemMark(LoggerFactory.class, "Logger Reloaded"); 114 } catch (Exception e) { 115 Logger.systemError(LoggerFactory.class, e); 116 } 117 } 118 }; 119 defaultConfig.load(loggerPropFile,updatedRun); 120 config(defaultConfig); 121 } 122 } 123 124 /** 125 * Check is started 126 * @return boolean 127 * */ 128 public static boolean isStarted() { 129 return LogRecord.isStarted(); 130 } 131 132 /** 133 * Check is loaded config 134 * @return boolean 135 * */ 136 public static boolean isLoadedConfig() { 137 return LOADED_CONFIG; 138 } 139 140 /** 141 * Get logger 142 * @return Logger 143 * @param clazz 144 * */ 145 public static Logger getLogger(Class clazz) { 146 return getLogger(clazz.getName()); 147 } 148 149 /** 150 * Get logger 151 * @return Logger 152 * @param clazz 153 * */ 154 public static Logger getLogger(String clazzName) { 155 return new Logger(clazzName); 156 } 157 158 public static ConfigProperties getConfigProperties(){ 159 return defaultConfig; 160 } 161 162}