1 /****************************************************************
2 * Licensed to the Apache Software Foundation (ASF) under one *
3 * or more contributor license agreements. See the NOTICE file *
4 * distributed with this work for additional information *
5 * regarding copyright ownership. The ASF licenses this file *
6 * to you under the Apache License, Version 2.0 (the *
7 * "License"); you may not use this file except in compliance *
8 * with the License. You may obtain a copy of the License at *
9 * *
10 * http://www.apache.org/licenses/LICENSE-2.0 *
11 * *
12 * Unless required by applicable law or agreed to in writing, *
13 * software distributed under the License is distributed on an *
14 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY *
15 * KIND, either express or implied. See the License for the *
16 * specific language governing permissions and limitations *
17 * under the License. *
18 ****************************************************************/
19 package org.apache.james.container.spring.logging.log4j;
20
21 import org.apache.avalon.framework.logger.Logger;
22 import org.apache.log4j.PropertyConfigurator;
23 import org.springframework.core.io.Resource;
24 import org.springframework.core.io.support.PropertiesLoaderUtils;
25
26 import java.io.IOException;
27 import java.net.URL;
28 import java.util.Properties;
29
30 /**
31 * simple default implementation. change the log4j configuration file to change specific logging behavior
32 */
33 public class LoggerToComponentMapper implements org.apache.james.container.spring.logging.LoggerToComponentMapper {
34 private Resource propertiesResource;
35
36 public LoggerToComponentMapper() {
37 }
38
39 public void setConfigurationResource(Resource propertiesResource) {
40 this.propertiesResource = propertiesResource;
41 }
42
43 public void init() {
44 Properties properties;
45 URL url = null;
46 try {
47 url = propertiesResource.getURL();
48 properties = PropertiesLoaderUtils.loadProperties(propertiesResource);
49 } catch (IOException e) {
50 throw new RuntimeException("failed to load log4j properties from " + url);
51 }
52 PropertyConfigurator.configure(properties);
53 }
54
55 public Logger getComponentLogger(String beanName) {
56 return new AvalonToLog4jLogger(org.apache.log4j.Logger.getLogger(beanName));
57 }
58 }