1, Logback.xml
<pre name= "code" class= "HTML" ><?xml version= "1.0" encoding= "UTF-8"?> <configuration> <!--APP NAM E--> <property name= "app_name value=" template "/> <!--log root DIR--> <property name=" Log_
Root_dir "value=" Logs/${app_name} "/> <!--formatted output. %d/date{hh:mm:ss.
SSS}: Print log for output log, schema syntax and Java.text.SimpleDateFormat compatible%-5p/le/level: level from left displays 5 character widths%t/thread output the thread name that generated the log.
%LOGGER{36} indicates that the logger name is 36 characters long and 0 indicates that only the string after the logger rightmost point symbol is entered%F java source filename *.java%l row%m/msg/message: Log contents %n: Line break--> <property name= "Encoder_pattern" value= "%d{[yyyy-mm-dd hh:mm:ss]}[%level][%logger{1}:%l"%msg%n "/> <property name=" ENCODING value= "UTF-8"/> <contextName>${APP_NAME}</contextName> &l T;jmxconfigurator/> <contextlistener class= "Ch.qos.logback.classic.jul.LevelChangePropagator" > <
Resetjul>true</resetjul> </contextListener> <!--output to console--> <appender name= "console" class= "Ch.qos.logback.core.ConsoleAppender" > <enco Der charset= "${encoding}" > <pattern>${ENCODER_PATTERN}</pattern> </encoder> & Lt;/appender> <!--Appender is a child node of <configuration> and is the component responsible for writing the log. There are two required attribute name and class. Name Specifies the Appender name, class specifies the fully qualified name of the Appender--> <appender name= "Template_file" Ch.qos.logback.core.rolling.RollingFileAppender ><!--Scroll through the file, logging to the specified file and logging to other files--> <fil when a condition is met E>${log_root_dir}/template.log</file> <encoding>${ENCODING}</encoding> <append>
;true</append> <!--true: The log is appended to the end of the file, false: Empty the existing file, the default is true--> <encoder> <!--format the logged event--> <pattern>${ENCODER_PATTERN}</pattern> </encoder> <!--filter, executing a filter will
There is an enumeration value returned, that is, one of the deny,neutral,accept. Returns deny, the log is immediately discarded and no longer passes through other filters; returns neutral, the next filter in the list is processed and the log is returned, and the log is processed immediately and no longer passes through the remaining filters. Accept --> <filter class= "Ch.qos.logback.classic.filter.LevelFilter" > <LEVEL>ERROR</LEVEL&G
T <onMatch>ACCEPT</onMatch> <!--is used to configure operations that meet the filtering conditions--> <ONMISMATCH>DENY</ONMISMATCH&G T <!--to configure operations that do not conform to the filter--> </filter> <rollingpolicy class= "ch.qos.logback.core.rolling.TimeBased Rollingpolicy "> <FILENAMEPATTERN>${LOG_ROOT_DIR}/TEMPLATE_%D{YYYY-MM-DD}.LOG.GZ</FILENAMEPATTERN&G t;<!--Compressed file name set--> </rollingPolicy> <!--log file maximum size--> <triggeringpolicy clas
s= "Ch.qos.logback.core.rolling.SizeBasedTriggeringPolicy" > <MaxFileSize>10MB</MaxFileSize> </triggeringPolicy> </appender> <!--log asynchronous to database--> <appender name= "DB" class= CH.QOS.L Ogback.classic.db.DBAppender "> <!--log to database asynchronously> <connectionsource class= "Ch.qos.logback.core.db.DriverManagerConnectionSource" > <!--connection pool --> <datasource class= "Com.mchange.v2.c3p0.ComboPooledDataSource" > <driverclass>
;com.mysql.jdbc.driver</driverclass> <url>jdbc:mysql://127.0.0.1:3306/databaseName</url> <user>root</user> <password>root</password> </data Source> </connectionSource> </appender> <!--mail Send related--> <property name= "smtph OST "value=" smtp.exmail.qq.com "/> <property name=" username "value=" xx@xx.com "/>" <property name= "Smtppo" RT "Value="/> <property name= "password" value= "xx"/> <property "SSL" Name= "false" value=/> T;property name= "email_to" value= "xx@xx.com"/> <property name= "Email_from" value= "xx@xx.com"/> <proper Ty name= "Email_subject"Value= "" Error:${app_name} "%logger{20}:%l"/> <appender name= "EMAIL" class= " Ch.qos.logback.classic.net.SMTPAppender "> <smtpHost>${smtpHost}</smtpHost> <smtpPort> ${smtpport}</smtpport> <username>${username}</username> <password>${password}</
Password> <SSL>${SSL}</SSL> <asynchronousSending>false</asynchronousSending> <to>${email_to}</to> <from>${email_from}</from> <subject>${email_subje ct}</subject> <layout class= "Ch.qos.logback.classic.html.HTMLLayout" > <pattern>%d{[y YYY-MM-DD hh:mm:ss]}[%level][%logger:%l]%msg</pattern> </layout> <filter class= "Ch.qos.log Back.core.filter.EvaluatorFilter "> <level>ERROR</level> <onmatch>accept</o Nmatch> <onmismatch>neutral</onmismatCh> </filter> </appender> <!--mail delivery related--> <root level= "DEBUG" > ; Appender-ref ref= "Template_file"/> <appender-ref ref= "Console"/> <!--<appender-ref ref= "DB "/> <appender-ref ref=" EMAIL/>--> </root> <!--Configuring a standalone package--> <logger name= "Co M.alibaba.dubbo "level=" WARN "> <appender-ref ref=" error_file "/> <appender-ref ref=" Console "/&G
T </logger> </configuration>
2, Controller
Import Com.saohuobang.payment.service.JMXConfigService;
Import Javax.annotation.Resource;
Import javax.management.InstanceAlreadyExistsException;
Import javax.management.MBeanRegistrationException;
Import javax.management.MalformedObjectNameException;
Import javax.management.NotCompliantMBeanException;
Import Org.springframework.stereotype.Controller;
Import org.springframework.web.bind.annotation.RequestMapping;
Import Org.springframework.web.bind.annotation.RequestParam;
Import Org.springframework.web.bind.annotation.ResponseBody; /** * Author Yaohua.liu * Date 2015-11-24 11:32 * Description Log Level Dynamic Control interface/@RequestMapping (value = "/log") @Controller public class
Jmxconfigcontroller extends backdoorcontroller{@Resource private Jmxconfigservice jmxconfigservice; @RequestMapping (value = "/startjmxconfig.api") @ResponseBody public Object startjmxconfig (@RequestParam required = False) String Port throws Malformedobjectnameexception, Notcompliantmbeanexception, InstancealreadyExistsexception, mbeanregistrationexception {String result = Jmxconfigservice.startjmxconfig (port);
return result; @RequestMapping (value = "/stopjmxconfig.api") @ResponseBody public Object stopjmxconfig () throws Exception
{String result = Jmxconfigservice.stopjmxconfig ();
return result;
}
}
3. Service
Import javax.management.InstanceAlreadyExistsException;
Import javax.management.MBeanRegistrationException;
Import javax.management.MalformedObjectNameException;
Import javax.management.NotCompliantMBeanException;
/**
* Author Yaohua.liu
* Date 2015-11-25 13:50
* Description ...
*
/public interface Jmxconfigservice {public
string Startjmxconfig (String port) throws Malformedobjectnameexception, Notcompliantmbeanexception, Instancealreadyexistsexception, mbeanregistrationexception;
Public String Stopjmxconfig () throws Exception;
}
4, IMPL implementation
Import Ch.qos.logback.classic.LoggerContext;
Import Ch.qos.logback.classic.jmx.JMXConfigurator;
Import com.google.common.base.Strings;
Import Com.saohuobang.payment.service.JMXConfigService;
Import Com.sun.jdmk.comm.HtmlAdaptorServer;
Import javax.management.InstanceAlreadyExistsException;
Import javax.management.MBeanRegistrationException;
Import Javax.management.MBeanServer;
Import Javax.management.MBeanServerFactory;
Import javax.management.MalformedObjectNameException;
Import javax.management.NotCompliantMBeanException;
Import Javax.management.ObjectName;
Import Org.slf4j.Logger;
Import Org.slf4j.LoggerFactory;
Import Org.springframework.stereotype.Service; /** * Author Yaohua.liu * Date 2015-11-25 14:23 * Description ... * * @Service public class Jmxconfigserviceimpl implements Jmxconfigs
Ervice {private Logger Logger = Loggerfactory.getlogger (Jmxconfigservice.class);
Public final String domain_name = "LOGBACK_JMX";
Public final String reload_config_name = "Reloadconfig"; PUblic final String connector_name = "Htmlconnector";
Private Mbeanserver Mbeanserver;
Private Jmxconfigurator Reloadconfig;
Private Htmladaptorserver connector;
Public Loggercontext Loggercontext = (loggercontext) loggerfactory.getiloggerfactory ();
private int defaultport = 10110; @Override public string Startjmxconfig (String portstr) throws Malformedobjectnameexception, Notcompliantmbeanexceptio N, Instancealreadyexistsexception, mbeanregistrationexception {mbeanserver = Mbeanserverfactory.creatembeanserver
(domain_name);
int port = 0;
try {if (Strings.isnullorempty (PORTSTR)) {port = Defaultport;
else {port = Integer.parseint (PORTSTR);
(Exception e) {logger.error ("format jmx port: {} failed, using default port: {}", Portstr, Defaultport);
Port = Defaultport; }//Registration service objectname on = new ObjectName (domain_name + ": name="+ Reload_config_name);
Reloadconfig = new Jmxconfigurator (Loggercontext, Mbeanserver, ON);
Mbeanserver.registermbean (Reloadconfig, new objectname (domain_name + ": name=" + reload_config_name));
Registered Connection connector = new Htmladaptorserver ();
Connector.setport (port);
Mbeanserver.registermbean (connector, new objectname (domain_name + ": name=" + connector_name));
Connector.start ();
return domain_name + ":" + port; @Override public String Stopjmxconfig () throws Exception {if connector!= null && CONNECTOR.I
Sactive ()) {connector.stop ();
} mbeanserver = null;
Reloadconfig = null;
connector = NULL;
Return "Stop success!";
}
}
5. Visit: Http://127.0.0.1:10001/log/startJMXConfig.api
Return: logback_jmx:10110, indicating that the port is open 10110
Pages can specify ports at request: http://127.0.0.1:10001/log/startJMXConfig.api?port=101010
6, open Address: http://127.0.0.1:10110/
7, click Name=reloadconfig, jump to: http://127.0.0.1:10110/ViewObjectRes//logback_jmx%3Aname%3DreloadConfig, scroll bar to the bottom, The display and operation are as follows: