| 1 | /** |
| 2 | * Licensed to jclouds, Inc. (jclouds) under one or more |
| 3 | * contributor license agreements. See the NOTICE file |
| 4 | * distributed with this work for additional information |
| 5 | * regarding copyright ownership. jclouds 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.jclouds.logging.log4j; |
| 20 | |
| 21 | import static org.apache.log4j.Level.ERROR; |
| 22 | import static org.apache.log4j.Level.WARN; |
| 23 | |
| 24 | import org.jclouds.logging.BaseLogger; |
| 25 | import org.jclouds.logging.Logger; |
| 26 | |
| 27 | /** |
| 28 | * {@link org.apache.log4j.Logger} implementation of {@link Logger}. |
| 29 | * |
| 30 | * @author Adrian Cole |
| 31 | * |
| 32 | */ |
| 33 | public class Log4JLogger extends BaseLogger { |
| 34 | private final org.apache.log4j.Logger logger; |
| 35 | private final String category; |
| 36 | |
| 37 | public static class Log4JLoggerFactory implements LoggerFactory { |
| 38 | public Logger getLogger(String category) { |
| 39 | return new Log4JLogger(category, org.apache.log4j.Logger |
| 40 | .getLogger(category)); |
| 41 | } |
| 42 | } |
| 43 | |
| 44 | public Log4JLogger(String category, org.apache.log4j.Logger logger) { |
| 45 | this.category = category; |
| 46 | this.logger = logger; |
| 47 | } |
| 48 | |
| 49 | @Override |
| 50 | protected void logTrace(String message) { |
| 51 | logger.trace(message); |
| 52 | } |
| 53 | |
| 54 | public boolean isTraceEnabled() { |
| 55 | return logger.isTraceEnabled(); |
| 56 | } |
| 57 | |
| 58 | @Override |
| 59 | protected void logDebug(String message) { |
| 60 | logger.debug(message); |
| 61 | } |
| 62 | |
| 63 | public boolean isDebugEnabled() { |
| 64 | return logger.isDebugEnabled(); |
| 65 | } |
| 66 | |
| 67 | @Override |
| 68 | protected void logInfo(String message) { |
| 69 | logger.info(message); |
| 70 | } |
| 71 | |
| 72 | public boolean isInfoEnabled() { |
| 73 | return logger.isInfoEnabled(); |
| 74 | } |
| 75 | |
| 76 | @Override |
| 77 | protected void logWarn(String message) { |
| 78 | logger.warn(message); |
| 79 | } |
| 80 | |
| 81 | @Override |
| 82 | protected void logWarn(String message, Throwable e) { |
| 83 | logger.warn(message, e); |
| 84 | } |
| 85 | |
| 86 | public boolean isWarnEnabled() { |
| 87 | return logger.isEnabledFor(WARN); |
| 88 | } |
| 89 | |
| 90 | @Override |
| 91 | protected void logError(String message) { |
| 92 | logger.error(message); |
| 93 | } |
| 94 | |
| 95 | @Override |
| 96 | protected void logError(String message, Throwable e) { |
| 97 | logger.error(message, e); |
| 98 | } |
| 99 | |
| 100 | public boolean isErrorEnabled() { |
| 101 | return logger.isEnabledFor(ERROR); |
| 102 | } |
| 103 | |
| 104 | public String getCategory() { |
| 105 | return category; |
| 106 | } |
| 107 | } |