Activiti中的log4j(slf4j)的配置

lonecloud發表於2016-08-23

今天試了一下在Activiti中使用log4j來進行配置發現這個會出現問題,其實Activiti中的日誌系統是採用的是slf4j而不是log4j

然後使用slf4j驅動log4j來做的

通過ProcessEngineImpl中的原始碼可以看出

  1 /* Licensed under the Apache License, Version 2.0 (the "License");
  2  * you may not use this file except in compliance with the License.
  3  * You may obtain a copy of the License at
  4  * 
  5  *      http://www.apache.org/licenses/LICENSE-2.0
  6  * 
  7  * Unless required by applicable law or agreed to in writing, software
  8  * distributed under the License is distributed on an "AS IS" BASIS,
  9  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 10  * See the License for the specific language governing permissions and
 11  * limitations under the License.
 12  */
 13 package org.activiti.engine.impl;
 14 
 15 import java.util.Map;
 16 
 17 import org.activiti.engine.FormService;
 18 import org.activiti.engine.HistoryService;
 19 import org.activiti.engine.IdentityService;
 20 import org.activiti.engine.ManagementService;
 21 import org.activiti.engine.ProcessEngine;
 22 import org.activiti.engine.ProcessEngines;
 23 import org.activiti.engine.RepositoryService;
 24 import org.activiti.engine.RuntimeService;
 25 import org.activiti.engine.TaskService;
 26 import org.activiti.engine.impl.cfg.ProcessEngineConfigurationImpl;
 27 import org.activiti.engine.impl.cfg.TransactionContextFactory;
 28 import org.activiti.engine.impl.el.ExpressionManager;
 29 import org.activiti.engine.impl.interceptor.CommandExecutor;
 30 import org.activiti.engine.impl.interceptor.SessionFactory;
 31 import org.activiti.engine.impl.jobexecutor.JobExecutor;
 32 import org.slf4j.Logger;
 33 import org.slf4j.LoggerFactory;
 34 
 35 /**
 36  * @author Tom Baeyens
 37  */
 38 public class ProcessEngineImpl implements ProcessEngine {
 39 
 40   private static Logger log = LoggerFactory.getLogger(ProcessEngineImpl.class);
 41 
 42   protected String name;
 43   protected RepositoryService repositoryService;
 44   protected RuntimeService runtimeService;
 45   protected HistoryService historicDataService;
 46   protected IdentityService identityService;
 47   protected TaskService taskService;
 48   protected FormService formService;
 49   protected ManagementService managementService;
 50   protected JobExecutor jobExecutor;
 51   protected CommandExecutor commandExecutor;
 52   protected Map<Class<?>, SessionFactory> sessionFactories;
 53   protected ExpressionManager expressionManager;
 54   protected TransactionContextFactory transactionContextFactory;
 55   protected ProcessEngineConfigurationImpl processEngineConfiguration;
 56 
 57   public ProcessEngineImpl(ProcessEngineConfigurationImpl processEngineConfiguration) {
 58     this.processEngineConfiguration = processEngineConfiguration;
 59     this.name = processEngineConfiguration.getProcessEngineName();
 60     this.repositoryService = processEngineConfiguration.getRepositoryService();
 61     this.runtimeService = processEngineConfiguration.getRuntimeService();
 62     this.historicDataService = processEngineConfiguration.getHistoryService();
 63     this.identityService = processEngineConfiguration.getIdentityService();
 64     this.taskService = processEngineConfiguration.getTaskService();
 65     this.formService = processEngineConfiguration.getFormService();
 66     this.managementService = processEngineConfiguration.getManagementService();
 67     this.jobExecutor = processEngineConfiguration.getJobExecutor();
 68     this.commandExecutor = processEngineConfiguration.getCommandExecutor();
 69     this.sessionFactories = processEngineConfiguration.getSessionFactories();
 70     this.transactionContextFactory = processEngineConfiguration.getTransactionContextFactory();
 71     
 72     commandExecutor.execute(processEngineConfiguration.getSchemaCommandConfig(), new SchemaOperationsProcessEngineBuild());
 73 
 74     if (name == null) {
 75       log.info("default activiti ProcessEngine created");
 76     } else {
 77       log.info("ProcessEngine {} created", name);
 78     }
 79     
 80     ProcessEngines.registerProcessEngine(this);
 81 
 82     if ((jobExecutor != null) && (jobExecutor.isAutoActivate())) {
 83       jobExecutor.start();
 84     }
 85     
 86     if (processEngineConfiguration.getProcessEngineLifecycleListener() != null) {
 87       processEngineConfiguration.getProcessEngineLifecycleListener().onProcessEngineBuilt(this);
 88     }
 89   }
 90   
 91   public void close() {
 92     ProcessEngines.unregister(this);
 93     if ((jobExecutor != null) && (jobExecutor.isActive())) {
 94       jobExecutor.shutdown();
 95     }
 96 
 97     commandExecutor.execute(processEngineConfiguration.getSchemaCommandConfig(), new SchemaOperationProcessEngineClose());
 98     
 99     if (processEngineConfiguration.getProcessEngineLifecycleListener() != null) {
100       processEngineConfiguration.getProcessEngineLifecycleListener().onProcessEngineClosed(this);
101     }
102   }
103 
104   // getters and setters //////////////////////////////////////////////////////
105 
106   public String getName() {
107     return name;
108   }
109 
110   public IdentityService getIdentityService() {
111     return identityService;
112   }
113 
114   public ManagementService getManagementService() {
115     return managementService;
116   }
117 
118   public TaskService getTaskService() {
119     return taskService;
120   }
121 
122   public HistoryService getHistoryService() {
123     return historicDataService;
124   }
125 
126   public RuntimeService getRuntimeService() {
127     return runtimeService;
128   }
129   
130   public RepositoryService getRepositoryService() {
131     return repositoryService;
132   }
133   
134   public FormService getFormService() {
135     return formService;
136   }
137 
138   public ProcessEngineConfigurationImpl getProcessEngineConfiguration() {
139     return processEngineConfiguration;
140   }
141 }
View Code

這兩個有啥區別?自己google吧

pom.xml

 1         <!-- log4j -->
 2         <!-- https://mvnrepository.com/artifact/log4j/log4j -->
 3         <dependency>
 4             <groupId>log4j</groupId>
 5             <artifactId>log4j</artifactId>
 6             <version>1.2.17</version>
 7         </dependency>
 8         <!-- slf4j -->
 9         <dependency>
10             <groupId>org.slf4j</groupId>
11             <artifactId>slf4j-log4j12</artifactId>
12             <version>1.7.2</version>
13         </dependency>

新增log4j.properties

### set log levels ###
log4j.rootLogger = debug ,  stdout

### \u8F93\u51FA\u5230\u63A7\u5236\u53F0 ###
log4j.appender.stdout = org.apache.log4j.ConsoleAppender
log4j.appender.stdout.Target = System.out
log4j.appender.stdout.layout = org.apache.log4j.PatternLayout
log4j.appender.stdout.layout.ConversionPattern = %-d{yyyy-MM-dd HH:mm:ss}  [ %t:%r ] - [ %p ]  %m%n

### \u8F93\u51FA\u5230\u65E5\u5FD7\u6587\u4EF6 ###
log4j.appender.D = org.apache.log4j.DailyRollingFileAppender
log4j.appender.D.File = logs/error.log \#\# \u5F02\u5E38\u65E5\u5FD7\u6587\u4EF6\u540D
log4j.appender.D.Append = true
log4j.appender.D.Threshold = ERROR \#\# \u53EA\u8F93\u51FAERROR\u7EA7\u522B\u4EE5\u4E0A\u7684\u65E5\u5FD7\!\!\!
log4j.appender.D.layout = org.apache.log4j.PatternLayout
log4j.appender.D.layout.ConversionPattern = %-d{yyyy-MM-dd HH:mm:ss}  [ %t:%r ] - [ %p ]  %m%n
View Code

然後就可以使用了

你只要在java檔案這樣寫就可以了

 1 package cn.lonecloud.mavenActivi;
 2 
 3 import org.activiti.engine.ProcessEngine;
 4 import org.activiti.engine.ProcessEngines;
 5 import org.activiti.engine.RuntimeService;
 6 import org.activiti.engine.TaskService;
 7 import org.activiti.engine.runtime.ProcessInstance;
 8 //使用log4j匯入的包
 9 import org.apache.log4j.Logger;
10 import org.junit.Test;
11 //使用slf4j的匯入的包
12 //import org.slf4j.Logger;
13 //import org.slf4j.LoggerFactory;
14 
15 public class InstanceDemo {
16     Logger logger=Logger.getLogger(this.getClass());
17 //    Logger logger=LoggerFactory.getLogger(this.getClass());
18     ProcessEngine processEngine = ProcessEngines.getDefaultProcessEngine();
19     RuntimeService runtimeService = processEngine.getRuntimeService();
20     TaskService taskService=processEngine.getTaskService();
21     @Test
22     public void testInstance(){
23         ProcessInstance processInstance = runtimeService.startProcessInstanceByKey("Instance","Test02");
24         logger.debug("獲取例項id"+processInstance.getId());
25     }
26 }

 

相關文章