KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > exoplatform > services > log > impl > LogServiceImpl


1 package org.exoplatform.services.log.impl;
2
3 import java.util.*;
4 import org.apache.commons.logging.Log;
5 import org.exoplatform.services.log.LogService;
6
7 /**
8  * Copyright 2001-2003 The eXo platform SARL All rights reserved.
9  * Please look at license.txt in info directory for more license detail.
10  **/

11
12 /**
13  * Created by the Exo Development team. Author : Mestrallet Benjamin
14  * benjmestrallet@users.sourceforge.net Date: 14 nov. 2003 Time: 20:40:57
15  */

16 public class LogServiceImpl implements LogService {
17
18     private HashMap logs_;
19
20     private HashMap configure_;
21
22     public LogServiceImpl() {
23         logs_ = new HashMap();
24         configure_ = new HashMap();
25         configure_.put("org.exoplatform.portal", new Integer JavaDoc(ExoLog.INFO));
26         configure_.put("org.exoplatform.services.portletcontainer",
27                 new Integer JavaDoc(ExoLog.INFO));
28     }
29
30     public void start() {
31     }
32
33     public void stop() {
34
35     }
36
37     public Log getLog(String JavaDoc name) {
38         Log log = (Log) logs_.get(name);
39         if (log == null) {
40             synchronized (logs_) {
41                 int level = ExoLog.INFO;
42                 try {
43                     level = getDefaultLogLevel(name);
44                 } catch (Exception JavaDoc ex) {
45                 }
46                 log = new ExoLog(name, level);
47                 logs_.put(name, log);
48             }
49         }
50         return log;
51     }
52
53     public Log getLog(Class JavaDoc clazz) {
54         String JavaDoc name = clazz.getName();
55         int idx = name.lastIndexOf(".");
56         name = name.substring(0, idx);
57         return getLog(name);
58     }
59
60     public Collection getLogs() {
61         return logs_.values();
62     }
63
64     public int getLogLevel(String JavaDoc name) throws Exception JavaDoc {
65         ExoLog log = (ExoLog) logs_.get(name);
66         if (log != null)
67             return log.getLevel();
68         return INFO;
69     }
70
71     public void setLogLevel(String JavaDoc name, int level, boolean recursive)
72             throws Exception JavaDoc {
73         if (recursive) {
74             Iterator i = logs_.values().iterator();
75             while (i.hasNext()) {
76                 ExoLog log = (ExoLog) i.next();
77                 if (log.getLogCategory().startsWith(name)) {
78                     log.setLevel(level);
79                 }
80             }
81         } else {
82             ExoLog log = (ExoLog) logs_.get(name);
83             if (log != null) {
84                 log.setLevel(level);
85             }
86         }
87     }
88
89     public List getLogBuffer() {
90         return ExoLog.getLogBuffer();
91     }
92
93     public List getErrorBuffer() {
94         return ExoLog.getErrorBuffer();
95     }
96
97     private int getDefaultLogLevel(String JavaDoc name) throws Exception JavaDoc {
98         while (name != null) {
99             Integer JavaDoc level = (Integer JavaDoc) configure_.get(name);
100             if (level != null)
101                 return level.intValue();
102             int index = name.lastIndexOf(".");
103             if (index > 0)
104                 name = name.substring(0, index);
105             else
106                 name = null;
107         }
108         return ExoLog.INFO;
109     }
110 }
Popular Tags