2 * Copyright (C) 2014 - Christian Babeux <christian.babeux@efficios.com>
5 * This library is free software; you can redistribute it and/or modify it
6 * under the terms of the GNU Lesser General Public License, version 2.1 only,
7 * as published by the Free Software Foundation.
9 * This library is distributed in the hope that it will be useful, but WITHOUT
10 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
11 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License
14 * You should have received a copy of the GNU Lesser General Public License
15 * along with this library; if not, write to the Free Software Foundation,
16 * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
19 package org
.lttng
.ust
.agent
.log4j
;
21 import java
.util
.Enumeration
;
22 import java
.util
.Iterator
;
23 import java
.util
.Vector
;
25 import org
.apache
.log4j
.LogManager
;
26 import org
.apache
.log4j
.Logger
;
27 import org
.lttng
.ust
.agent
.LogFrameworkSkeleton
;
30 * log4j logging framework
32 * @author Christian Babeux
34 public class LTTngLog4j
extends LogFrameworkSkeleton
{
36 private LTTngLogAppender appender
;
37 private Boolean attached
;
43 * If this logger is a root logger or not.
45 public LTTngLog4j(Boolean isRoot
) {
47 this.appender
= new LTTngLogAppender(isRoot
);
48 this.attached
= false;
52 public Boolean
enableLogger(String name
) {
53 if(!super.enableLogger(name
)) {
57 /* The first enable of any event triggers the attachment to the root logger */
58 if (getEventCount() == 1 && !this.attached
) {
66 public Boolean
disableLogger(String name
) {
67 if(!super.disableLogger(name
)) {
71 /* Detach from the root logger when the event counts reach zero */
72 if (getEventCount() == 0 && this.attached
) {
73 detachFromRootLogger();
80 public Iterator
<String
> listLoggers() {
81 Vector
<String
> logs
= new Vector
<String
>();
82 for (Enumeration
<?
> loggers
= LogManager
.getCurrentLoggers(); loggers
.hasMoreElements(); ) {
83 Logger logger
= (Logger
) loggers
.nextElement();
84 String name
= logger
.getName();
88 return logs
.iterator();
92 public Boolean
isRoot() {
93 return appender
.isRoot();
99 detachFromRootLogger();
102 private void attachToRootLogger() {
107 Logger logger
= Logger
.getRootLogger();
108 logger
.addAppender(this.appender
);
109 this.attached
= true;
112 private void detachFromRootLogger() {
113 if (!this.attached
) {
117 Logger logger
= Logger
.getRootLogger();
118 logger
.removeAppender(this.appender
);
119 this.attached
= false;