In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-01-17 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Internet Technology >
Share
Shulou(Shulou.com)06/01 Report--
This article introduces the "web log source code analysis" related knowledge, in the actual case operation process, many people will encounter such a dilemma, and then let the editor lead you to learn how to deal with these situations! I hope you can read it carefully and be able to achieve something!
The relationship between logs can be sorted out through this picture.
Both commons-logging and slf4j are log interfaces for users to use, but no implementation is provided!
Log4j,logback and so on are the real implementation of logging.
Current logging frameworks include logging,log4j1, log4j2 and logback that come with jdk.
Commons-logging and slf4j of apache, a framework currently used to unify logs
In order to sort out their relationship, with a variety of complex integrated jar packages, as follows:
Log4j 、 log4j-api 、 log4j-core
Log4j-1.2-api 、 log4j-jcl 、 log4j-slf4j-impl 、 log4j-jul
Logback-core 、 logback-classic 、 logback-access
Commons-logging
Slf4j-api 、 slf4j-log4j12 、 slf4j-simple 、 jcl-over-slf4j 、 slf4j-jdk14 、 log4j-over-slf4j 、 slf4j-jcl
Log that comes with 1.jdk
Java.util.logging.Logger l = java.util.logging.Logger.getLogger (Deme.class.getName ())
Check the source code, mainly to read the configuration file when building LoggerManage
Public static LogManager getLogManager () {if (manager! = null) {manager.ensureLogManagerInitialized ();} return manager;} final void ensureLogManagerInitialized () {final LogManager owner = this;// omitted / / Read configuration. Owner.readPrimordialConfiguration ();} private void readPrimordialConfiguration () {/ / omit readConfiguration ();} public void readConfiguration () throws IOException. SecurityException {/ / omit / / default is the lib/logging.properties file in the jre directory. You can also customize and modify the system attribute "java.util.logging.config.file". The source code is as follows: String fname = System.getProperty ("java.util.logging.config.file"). If (fname = = null) {fname = System.getProperty ("java.home"); if (fname = = null) {throw new Error ("Can't find java.home?");} File f = new File (fname, "lib"); f = new File (f, "logging.properties"); fname = f.getCanonicalPath () } try (final InputStream in = new FileInputStream (fname)) {final BufferedInputStream bin = new BufferedInputStream (in); readConfiguration (bin);}}
2.1 log4j1 really implements log reading and writing
/ / jar package introduces log4j log4j 1.2.17 class or className / initialization, look at the source code, org.apache.log4j.Logger logger1 = org.apache.log4j.Logger.getLogger ("class or className"); public class Logger extends Category {/ / you can see that Logger inherits the Category class, which is useful when printing log information. Logger getLogger (String name) {return LogManager.getLogger (name);}} public class LogManager {/ / mainly LogManager's getLogger method Logger getLogger (final String name) {/ / Delegate the actual manufacturing of the logger to the logger repository. The return getLoggerRepository (). GetLogger (name);} / LogManager class has a static method static {/ / initializes a logger repository Hierarchy and then binds it to LoggerManager, mainly through the getLoggerRepository () method. / / By default we use a DefaultRepositorySelector which always returns'hobby. Hierarchy h = new Hierarchy (new RootLogger ((Level) Level.DEBUG)); repositorySelector = new DefaultRepositorySelector (h); / * * Search for the properties file log4j.properties in the CLASSPATH. * / String override = OptionConverter.getSystemProperty (DEFAULT_INIT_OVERRIDE_KEY, null); / / if there is no default init override, then get the resource / / specified by the user or the default config file. If (override = = null | | "false" .equalsIgnoreCase (override)) {String configurationOptionStr = OptionConverter.getSystemProperty (DEFAULT_CONFIGURATION_KEY, null); / / omitted code / / the configuration file has a loading order / / log4j.defaultInitOverride > log4j.configuration > log4j.xml > log4j.properties} / / by viewing the class Hierarchy} public class Hierarchy implements LoggerRepository, RendererSupport, ThrowableRendererSupport {private LoggerFactory defaultFactory; / / create Logger factory Hashtable ht; / / store the Logger Logger root created by the factory / / used to carry the results of parsing configuration files, set the level, and store appender / / omit / / constructor public Hierarchy (Logger root) {ht = new Hashtable (); listeners = new Vector (1); this.root = root; / / Enable all level levels by default. SetThreshold (Level.ALL); this.root.setHierarchy (this); rendererMap = new RendererMap (); defaultFactory = new DefaultCategoryFactory ();}}
2.2 log4j2 log source code parsing
/ / add jar package, org.apache.logging.log4j log4j-api 2.2 org.apache.logging.log4j log4j-core 2.2og4j2 is divided into two parts: log4j-api: as the log interface layer, used to unify the underlying log system log4j-core: as the implementation of the above log interface Is an actual logging framework web.xml to add the following information org.apache.logging.log4j.web.Log4jServletFilter log4jServletFilter org.apache.logging.log4j.web.Log4jServletFilter log4jServletFilter / * REQUEST FORWARD INCLUDE ERROR / / create object Logger logger = LoggerFactory.getLogger (DemeMapping.class) / / getlogger method of LoggerFactory public final class LoggerFactory {public static Logger getLogger (Class clazz) {Logger logger = getLogger (clazz.getName ()); if (DETECT_LOGGER_NAME_MISMATCH) {Class autoComputedCallingClass = Util.getCallingClass (); if (nonMatchingClasses (clazz, autoComputedCallingClass)) {Util.report (String.format ("Detected logger name mismatch. Given name:\ "% s\"; computed name:\ "% s\"., logger.getName (), autoComputedCallingClass.getName ()); Util.report ("See http://www.slf4j.org/codes.html#loggerNameMismatch for an explanation");}} return logger } / / get the logger object public static Logger getLogger (String name) {/ / finally get the Log4jLoggerFactory object to implement the ILoggerFactory interface ILoggerFactory iLoggerFactory = getILoggerFactory (); / / actually call the class Log4jLoggerFactory return iLoggerFactory.getLogger (name);} public static ILoggerFactory getILoggerFactory () {/ / initialize and change INITIALIZATION_STATE = 3 performInitialization () Case 3: return StaticLoggerBinder.getSingleton (). GetLoggerFactory ();} / / look at the bind method private static final void bind () {String msg; try {/ / find the StaticLoggerBinder object through class loading here, and / / define the path in the code: "org/slf4j/impl/StaticLoggerBinder.class" >
3. Logback log resolution
Required jar package
Logback-core
Logback-classic
Slf4j-api
/ / add maven dependency ch.qos.logback logback-core 1.1.3 ch.qos.logback logback-classic 1.1.3 org.slf4j slf4j-api 1.7.12 framework.pisces pisces-log 1.2.3-beta Com.fasterxml.jackson.module jackson-module-jaxb-annotations com.fasterxml.jackson.dataformat jackson-dataformat-xml Com.fasterxml.jackson.module jackson-module-jaxb-annotations com.fasterxml.jackson.jaxrs jackson-jaxrs-xml-provider slf4j-log4j12 org.slf4j Log4j log4j / / declare the variable org.slf4j.Logger logger2 = LoggerFactory.getLogger ("") / / LoggerFactory is also used to create public final class LoggerFactory {public static Logger getLogger (String name) {ILoggerFactory iLoggerFactory = getILoggerFactory (); return iLoggerFactory.getLogger (name);}} / / the call here is the same as slf4j getILoggerFactory ()-> performInitialization ();-> bind ();-> findPossibleStaticLoggerBinderPathSet ();-> return LoggerContext object / / the only difference is that the address of the construction StaticLoggerBinder is "org/slf4j/impl/StaticLoggerBinder.class", when calling StaticLoggerBinder.getSingleton () Using StaticLoggerBinder public class StaticLoggerBinder implements LoggerFactoryBinder {/ / under the logback-classic-0.9.21.jar package, you can see that the LoggerContext object private LoggerContext defaultLoggerContext = new LoggerContext () is initialized and assigned here; static {SINGLETON.init ();} void init () {try {/ / here is the initialization and storage value defaultLoggerContext. Take a look at the autoConfig () method, which is mainly findURLOfDefaultConfigurationFile (true). Method in reading the configuration file, new ContextInitializer (defaultLoggerContext). AutoConfig ();} private static StaticLoggerBinder SINGLETON = new StaticLoggerBinder (); public static StaticLoggerBinder getSingleton () {return SINGLETON;} private StaticLoggerBinder () {defaultLoggerContext.setName (CoreConstants.DEFAULT_CONTEXT_NAME);}} "web log source code analysis". Thank you for reading. If you want to know more about the industry, you can follow the website, the editor will output more high-quality practical articles for you!
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.