Java log4j-based Logging tool class

Source: Internet
Author: User

A simple encapsulation of the Log4j log class, the advantage of using this encapsulation class is the following two points:

1, do not have to create objects in each class, the direct class name + method can

2, can be very convenient to print out the stack information

  

Package Com.tradeplatform.receiveorder.util;import Java.io.printwriter;import Java.io.stringwriter;import org.apache.log4j.logger;/** * @Description log record class * @author Panteng * @version V0.0.1 * @date 2016-09-08 */public class Sys        Log {public static Logger log = Logger.getlogger (Syslog.class); /** * Print Warning * * @param obj */public static void Warn (Object obj) {try{/*** get output information            The position of the code ***/String location = "";            Stacktraceelement[] Stacks = Thread.CurrentThread (). Getstacktrace ();  Location = Stacks[2].getclassname () + "." + stacks[2].getmethodname () + "(" + stacks[2].getlinenumber ()            + ")";                Whether the/*** is an exception ***/if (obj instanceof Exception) {Exception e = (Exception) obj;                StringWriter SW = new StringWriter ();                E.printstacktrace (New PrintWriter (SW, True));                String str = sw.tostring (); Log.warn (location + STR);            } else {Log.warn (location + obj.tostring ());        }}catch (Exception e) {//Todo:handle Exception e.printstacktrace (); }}/** * Print information * * @param obj */public static void info (Object obj) {try{/*            * * The location of the code that gets the output information ***/String position = "";            Stacktraceelement[] Stacks = Thread.CurrentThread (). Getstacktrace ();  Location = Stacks[2].getclassname () + "." + stacks[2].getmethodname () + "(" + stacks[2].getlinenumber ()            + ")";                Whether the/*** is an exception ***/if (obj instanceof Exception) {Exception e = (Exception) obj;                StringWriter SW = new StringWriter ();                E.printstacktrace (New PrintWriter (SW, True));                String str = sw.tostring ();            Log.info (location + str);            } else {Log.info (location + obj.tostring ());        }}catch (Exception e) {//Todo:handle Exception e.printstacktrace (); }}/** * Print error * * @param obj */public static void error (Object obj) {try{/            Gets the position of the code for the output information ***/String location = "";            Stacktraceelement[] Stacks = Thread.CurrentThread (). Getstacktrace ();  Location = Stacks[2].getclassname () + "." + stacks[2].getmethodname () + "(" + stacks[2].getlinenumber ()                        + ")";                Whether the/*** is an exception ***/if (obj instanceof Exception) {Exception e = (Exception) obj;                StringWriter SW = new StringWriter ();                E.printstacktrace (New PrintWriter (SW, True));                String str = sw.tostring ();            Log.error (location + str);            } else {Log.error (location + obj.tostring ()); }}catch (Exception e) {//Todo:handle Exception E.printstacKtrace ();            }}/** * insert information into database alarm table * @param obj */public static void Dbwarn (Object obj) {try{            String printinfo = "";            /*** the location of the code that gets the output information ***/String position = "";            Stacktraceelement[] Stacks = Thread.CurrentThread (). Getstacktrace ();  Location = Stacks[2].getclassname () + "." + stacks[2].getmethodname () + "(" + stacks[2].getlinenumber ()                        + ")";                Whether the/*** is an exception ***/if (obj instanceof Exception) {Exception e = (Exception) obj;                Printinfo = location + e.getmessage ();            Log.fatal (printinfo.substring (0, printinfo.length () > 512?512:printinfo.length ()));                } else {printinfo = location + obj.tostring ();            Log.fatal (printinfo.substring (0, printinfo.length () > 512?512:printinfo.length ()));     }}catch (Exception e) {//Todo:handle Exception       E.printstacktrace ();        }}/** * Gets the location of the code that called this function * @return package name. Class Name. Method name (number of rows) */public static String Getcodelocation () {            try{/*** Gets the position of the code for the output information ***/String location = "";            Stacktraceelement[] Stacks = Thread.CurrentThread (). Getstacktrace ();  Location = Stacks[2].getclassname () + "." + stacks[2].getmethodname () + "(" + stacks[2].getlinenumber ()            + ")";        return location;            }catch (Exception e) {//Todo:handle Exception Syslog.error (e);        Return ""; }    }}

Configuration file:

  

Base.logs.dir=d:/testmycatloglog4j.rootlogger = DEBUG, INFO, All_log, stdout, dblog4j.appender.stdout= org.apache.log4j.consoleappenderlog4j.appender.stdout.layout= Org.apache.log4j.patternlayoutlog4j.appender.stdout.layout.conversionpattern=%d{yyyy-mm-dd HH:mm:ss SSS}%-5p [%- 15X] [%t]-%m%nlog4j.appender.all_log=org.apache.log4j.dailyrollingfileappenderlog4j.appender.all_log. Datepattern= '. ' Yyyy-mm-dd-hhlog4j.appender.all_log. file=${base.logs.dir}/all_log.loglog4j.appender.all_log.layout= Org.apache.log4j.patternlayoutlog4j.appender.all_log.layout.conversionpattern=%d{yyyy-mm-dd HH:mm:ss SSS}%-5p [%- 15X] [%t]-%m%nlog4j.appender.db = org.apache.log4j.jdbc.jdbcappenderlog4j.appender.db.buffersize= 1log4j.appender.db.driver=com.mysql.jdbc.driverlog4j.appender.db.url=jdbc:mysql://192.168.91.231:8066/ Ctpdblog4j.appender.db.user=ctplog4j.appender.db.password=ctplog4j.appender.db.threshold = FATALlog4j.appender.DB.layout = Org.apache.log4j.patternlayoutlog4j.appender.db.sql=insert into SyswarN (warn_level,warn_info) VALUES ("1", '%m '); #--------------Kafka configuration----------------#log4j. appender.kafkaappender= Org.apache.log4j.dailyrollingfileappenderlog4j.appender.kafkaappender.datepattern= '. ' yyyy-mm-dd-hhlog4j.appender.kafkaappender.file=${base.logs.dir}/server.loglog4j.appender.kafkaappender.layout= ORG.APACHE.LOG4J.PATTERNLAYOUTLOG4J.APPENDER.KAFKAAPPENDER.LAYOUT.CONVERSIONPATTERN=[%D]%p%m (%c)% Nlog4j.appender.statechangeappender= Org.apache.log4j.dailyrollingfileappenderlog4j.appender.statechangeappender.datepattern= '. ' yyyy-mm-dd-hhlog4j.appender.statechangeappender.file=${base.logs.dir}/ state-change.loglog4j.appender.statechangeappender.layout= ORG.APACHE.LOG4J.PATTERNLAYOUTLOG4J.APPENDER.STATECHANGEAPPENDER.LAYOUT.CONVERSIONPATTERN=[%D]%p%m (%c)% Nlog4j.appender.requestappender= Org.apache.log4j.dailyrollingfileappenderlog4j.appender.requestappender.datepattern= '. ' yyyy-mm-dd-hhlog4j.appender.requestappender.file=${base.logs.dir}/ Kafka-request.loglog4j.appender.requestappender.laYOUT=ORG.APACHE.LOG4J.PATTERNLAYOUTLOG4J.APPENDER.REQUESTAPPENDER.LAYOUT.CONVERSIONPATTERN=[%D]%p%m (%c)% Nlog4j.appender.cleanerappender= Org.apache.log4j.dailyrollingfileappenderlog4j.appender.cleanerappender.datepattern= '. ' yyyy-mm-dd-hhlog4j.appender.cleanerappender.file=${base.logs.dir}/ log-cleaner.loglog4j.appender.cleanerappender.layout= ORG.APACHE.LOG4J.PATTERNLAYOUTLOG4J.APPENDER.CLEANERAPPENDER.LAYOUT.CONVERSIONPATTERN=[%D]%p%m (%c)% Nlog4j.appender.controllerappender= Org.apache.log4j.dailyrollingfileappenderlog4j.appender.controllerappender.datepattern= '. ' yyyy-mm-dd-hhlog4j.appender.controllerappender.file=${base.logs.dir}/ controller.loglog4j.appender.controllerappender.layout= ORG.APACHE.LOG4J.PATTERNLAYOUTLOG4J.APPENDER.CONTROLLERAPPENDER.LAYOUT.CONVERSIONPATTERN=[%D]%p%m (%c)%n# Turn on All our debugging info#log4j.logger.kafka.producer.async.defaulteventhandler=debug, kafkaappender# Log4j.logger.kafka.client.clientutils=debug, Kafkaappender#log4j.logger.kafka.perf=debug,Kafkaappender#log4j.logger.kafka.perf.producerperformance$producerthread=debug, kafkaAppender# Log4j.logger.org.i0itec.zkclient.zkclient=debuglog4j.logger.kafka=info, Kafkaappenderlog4j.logger.kafka.network.requestchannel$=warn, Requestappenderlog4j.additivity.kafka.network.requestchannel$=false#log4j.logger.kafka.network.processor=trace , Requestappender#log4j.logger.kafka.server.kafkaapis=trace, requestappender# Log4j.additivity.kafka.server.kafkaapis=falselog4j.logger.kafka.request.logger=warn, Requestappenderlog4j.additivity.kafka.request.logger=falselog4j.logger.kafka.controller=trace, Controllerappenderlog4j.additivity.kafka.controller=falselog4j.logger.kafka.log.logcleaner=info, Cleanerappenderlog4j.additivity.kafka.log.logcleaner=falselog4j.logger.kafka.log.cleaner=info, Cleanerappenderlog4j.additivity.kafka.log.cleaner=falselog4j.logger.state.change.logger=trace, statechangeappenderlog4j.additivity.state.change.logger=false#---------------------------------------#

Java log4j-based Logging tool class

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.