Log4j-1.2.17 is used here
Package COM. jadyer. util; import Org. apache. commons. logging. log; import Org. apache. commons. logging. logfactory; /*** log tool class * @ see ================================== ========================================================== ========================================================== * @ see [usage:] * @ see: execute logutil at the location where you want to record logs. setxxxlogger (), and then logutil. getlogger (). info (".... ") * @ see Example: assume that an SSH2 project has an action method named userlogin (), which internally calls userservice, u Serservice calls userdao * @ see. logutil is used in userlogin (), userservice, and userdao. getlogger (). info (".... ") record the corresponding log information * @ see at this time, our log information will be automatically recorded in the ecpaycus-default.log file * @ see similarly, if we in userlogin () execute logutil In the first sentence of the method. setwebcuslogger () * @ see then the log is automatically written into the ecpaycus-webcus.log file, instead of writing to the ecpaycus-default.log * @ see ================================== ========================================================== ======================================== =======* @ Author Xuan Yu LogUtil.setCurrentLogger((Log)session.getAttribute("currentLog"))* @ SeeLogutil. getlogger (). Error ("the request is rejected or the request address is incorrect. The stack information is as follows", cause)
* @ See: at the end of the session, remember to passsession.setAttribute("currentLog", LogUtil.getLogger())
Mark the logger */public static void setcurrentlogger (log) {currentloggermap. set (log);}/*** in multi-thread scenarios, if you want to use defaultlogger, you 'd better call this method once before use */public static void setdefaultlogger () {currentloggermap. set (defaultlogger);}/*** database storage recorder */public static void setdatabaselogger () {currentloggermap. set (databaselogger);}/*** custom web logger */public static void setwebcuslogger () {currentloggermap. set (webcuslogger);}/*** reconciliation file logger */public static void setrecfilelogger () {currentloggermap. set (recfilelogger );}}
Below is log4j. Properties
#use Root for GobalConfiglog4j.rootLogger=DEBUG,CONSOLE,EMAILSENDlog4j.logger.defaultLogger=DEBUG,DEFAULT_LOGlog4j.logger.databaseLogger=DEBUG,DATABASE_LOGlog4j.logger.ecpaycusWebCusLogger=DEBUG,ECPAYCUS_WEBCUS_LOGlog4j.logger.ecpaycusRecFileLogger=DEBUG,ECPAYCUS_RECFILE_LOGlog4j.additivity.defaultLogger=truelog4j.additivity.databaseLogger=truelog4j.additivity.ecpaycusWebCusLogger=truelog4j.additivity.ecpaycusRecFileLogger=true#use ConsoleAppender for ConsoleOutlog4j.appender.CONSOLE=org.apache.log4j.ConsoleAppenderlog4j.appender.CONSOLE.Threshold=DEBUGlog4j.appender.CONSOLE.Target=System.outlog4j.appender.CONSOLE.layout=org.apache.log4j.PatternLayoutlog4j.appender.CONSOLE.layout.ConversionPattern=[%d{yyyyMMdd HH:mm:ss}][%t][%C{1}.%M]%m%n#use SMTPAppender for EmailSendlog4j.appender.EMAILSEND=org.apache.log4j.net.SMTPAppenderlog4j.appender.EMAILSEND.SMTPDebug=falselog4j.appender.EMAILSEND.Threshold=ERRORlog4j.appender.EMAILSEND.BufferSize=10log4j.appender.EMAILSEND.SMTPHost=smtp.yeah.netlog4j.appender.EMAILSEND.SMTPUsername=jadyerlog4j.appender.EMAILSEND.SMTPPassword=jady*****erlog4j.appender.EMAILSEND.From=jadyer@yeah.netlog4j.appender.EMAILSEND.To=log4j001@yeah.net,log4j002@yeah.net,log4j003@yeah.netlog4j.appender.EMAILSEND.Cc=log4j004@yeah.netlog4j.appender.EMAILSEND.Bcc=log4j005@yeah.netlog4j.appender.EMAILSEND.Subject=\u3010\u63d0\u9192\u3011\u4ea4\u6613\u524d\u7f6e\u7cfb\u7edf\u9047\u5230\u5f02\u5e38log4j.appender.EMAILSEND.layout=org.apache.log4j.PatternLayoutlog4j.appender.EMAILSEND.layout.ConversionPattern=[%d{yyyyMMdd HH:mm:ss}][%t][%C{1}.%M]%m%n#use DailyRollingFileAppender for DefauleLogFilelog4j.appender.DEFAULT_LOG=org.apache.log4j.DailyRollingFileAppenderlog4j.appender.DEFAULT_LOG.File=../logs/default.loglog4j.appender.DEFAULT_LOG.encoding=UTF-8log4j.appender.DEFAULT_LOG.Threshold=DEBUGlog4j.appender.DEFAULT_LOG.DatePattern='.'yyyyMMddlog4j.appender.DEFAULT_LOG.layout=org.apache.log4j.PatternLayoutlog4j.appender.DEFAULT_LOG.layout.ConversionPattern=[%d{yyyyMMdd HH:mm:ss}][%t][%C{1}.%M]%m%n#user JDBCAppender for DataBaseSavedlog4j.appender.DATABASE_LOG=org.apache.log4j.jdbc.JDBCAppenderlog4j.appender.DATABASE_LOG.URL=jdbc:mysql://127.0.0.1:3306/jadyer?characterEncoding=UTF-8log4j.appender.DATABASE_LOG.driver=com.mysql.jdbc.Driverlog4j.appender.DATABASE_LOG.user=rootlog4j.appender.DATABASE_LOG.password=jadyerlog4j.appender.DATABASE_LOG.sql=insert into OPERATE_LOG(message) values ('[%d{yyyyMMdd HH:mm:ss}][%t][%C{1}.%M]%m%n')log4j.appender.DATABASE_LOG.layout=org.apache.log4j.PatternLayoutlog4j.appender.DATABASE_LOG.layout.ConversionPattern=[%d{yyyyMMdd HH:mm:ss}][%t][%C{1}.%M]%m%n#use DailyRollingFileAppender for WebCusLogFilelog4j.appender.ECPAYCUS_WEBCUS_LOG=org.apache.log4j.DailyRollingFileAppenderlog4j.appender.ECPAYCUS_WEBCUS_LOG.File=../logs/ecpaycus-webcus.loglog4j.appender.ECPAYCUS_WEBCUS_LOG.encoding=UTF-8log4j.appender.ECPAYCUS_WEBCUS_LOG.Threshold=DEBUGlog4j.appender.ECPAYCUS_WEBCUS_LOG.DatePattern='.'yyyyMMddlog4j.appender.ECPAYCUS_WEBCUS_LOG.layout=org.apache.log4j.PatternLayoutlog4j.appender.ECPAYCUS_WEBCUS_LOG.layout.ConversionPattern=[%d{yyyyMMdd HH:mm:ss}][%t][%C{1}.%M]%m%n#use DailyRollingFileAppender for ReconciliationFilelog4j.appender.ECPAYCUS_RECFILE_LOG=org.apache.log4j.DailyRollingFileAppenderlog4j.appender.ECPAYCUS_RECFILE_LOG.File=../logs/ecpaycus-recfile.loglog4j.appender.ECPAYCUS_RECFILE_LOG.encoding=UTF-8log4j.appender.ECPAYCUS_RECFILE_LOG.Threshold=DEBUGlog4j.appender.ECPAYCUS_RECFILE_LOG.DatePattern='.'yyyyMMddlog4j.appender.ECPAYCUS_RECFILE_LOG.layout=org.apache.log4j.PatternLayoutlog4j.appender.ECPAYCUS_RECFILE_LOG.layout.ConversionPattern=[%d{yyyyMMdd HH:mm:ss}][%t][%C{1}.%M]%m%n
Finally, the test class for sending text messages
Package COM. jadyer. demo; import Java. io. ioexception; import COM. jadyer. util. logutil;/*** test: use log4j To Send Logs to your mailbox * @ see ==================================== ========================================================== ================================================== * @ See note: SMTP authentication is supported after the log4j-1.2.14, otherwise exceptions will be reported .... here I use the latest log4j-1.2.17.jar * @ see ================================ ========================================================== =========================================== * @ See In addition, many people on the Internet say that the sent mail is sent. If the subject and body have Chinese characters, the recipient will receive garbled characters * @ see is strange, the subject and body I sent out here are both Chinese, result All emails received by the recipient do not contain garbled characters * @ see ============================ ========================================================== ===========================================================* @ see program at runtime, You need to import mail.jar(you can download javamail-1.4.5.zip through the oracleofficial website to obtain ).... in addition, the program may report the following exceptions * @ See exception in thread "Main" Java. lang. noclassdeffounderror: COM/Sun/mail/util/lineinputstream * @ see this is because myec Java EE provided by lipse6.5. the mail package in jar, and the imported mail. jar conflict * @ see solution: Find javaee in the myeclipse installation directory. jar, open it with WinRAR, and delete the mail folder in it. * @ see .. \ myeclipse \ eclipse \ plugins \ COM. genuitec. eclipse. j2eedt. core_6.5.0.zmyeclipse0000200806 \ data \ libraryset \ ee_5 \ javaee. jar * @ see ============================================ ========================================================== =================================* @ see smtp.yeah.net used here is a third-party SMTP service, we can also Install an SMTP service, such as IMail and Postfix * @ see. In addition, the SMTP service is generally enabled for UNIX and Linux by default. You can run this command on Linux to view the following information: telnet 127.0.0.1 25 * @ see [wzf @ bjgg-kfvm-31 ~] $ Telnet 127.0.0.1 25 * @ see trying 127.0.0.1... * @ see connected to localhost. localdomain (127.0.0.1 ). * @ see escape character is '^]'. * @ see 220 bjgg-kfvm-31.localdomain ESMTP Postfix * @ see this indicates that it has enabled the SMTP service, but if you see words similar to the following, it indicates that the SMTP service * @ see is being connected to 127.0.0.1... the connection to the host cannot be opened on port 25: connection Failed * @ see ========================================== ========================================================== ===========================================* @ see In addition, for instructions on how to manually send emails through Java and send emails with attachments, can refer to my three blog posts * @ see mail sender V1.0 ==> http://blog.csdn.net/jadyer/article/details/6130088 * @ see mail sender program V2.0 ==> http://blog.csdn.net/jadyer/article/details/6130105 * @ see mail sender program V3.0 ==> http://blog.csdn.net/jadyer/article/details/6130131 * @ see ============================================== ========================================================== =======================================* @ author Xuan Yu