View Javadoc

1   
2   /*
3    * SmartCrawler
4    *
5    * $Id: SCLoggerFactory.java,v 1.8 2005/08/05 15:55:53 vincool Exp $
6    * Copyright 2005 Davide Pozza
7    *
8    * This program is free software; you can redistribute it
9    * and/or modify it under the terms of the GNU General Public
10   * License as published by the Free Software Foundation;
11   * either version 2 of the License, or (at your option) any
12   * later version.
13   *
14   * This program is distributed in the hope that it will be
15   * useful, but WITHOUT ANY WARRANTY; without even the implied
16   * warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
17   * PURPOSE. See the GNU General Public License for more
18   * details.
19   *
20   * You should have received a copy of the GNU General Public
21   * License along with this program; if not, write to the Free
22   * Software Foundation, Inc., 59 Temple Place, Suite 330,
23   * Boston, MA 02111-1307 USA
24   *
25   */
26  
27  package org.smartcrawler.common;
28  
29  import java.util.Map;
30  import org.apache.log4j.BasicConfigurator;
31  import org.apache.log4j.ConsoleAppender;
32  import org.apache.log4j.Layout;
33  import org.apache.log4j.Level;
34  import org.apache.log4j.Logger;
35  import org.apache.log4j.PatternLayout;
36  import org.apache.log4j.RollingFileAppender;
37  import org.apache.log4j.spi.LoggerFactory;
38  
39  /***
40   * Custom Factory.
41   *
42   * @author <a href="mailto:pozzad@alice.it">Davide Pozza</a>
43   * @version <tt>$Revision: 1.8 $</tt>
44   */
45  public class SCLoggerFactory implements LoggerFactory {
46  
47      /***
48       * The constructor should be public as it will be called by
49       * configurators in different packages.
50       */
51      public SCLoggerFactory(Map lg) {
52          if (lg.containsKey("TRACER")) {
53              initLogger(
54                      new PatternLayout("[%d] %t %c %-5p - %m%n"),
55                      null,
56                      "smartcrawler.log",
57                      ("" + lg.get("TRACER")).equals("yes")
58                      );
59          }
60          if (lg.containsKey("ACCESS")) {
61              initLogger(
62                      new PatternLayout("[%d] - %m%n"),
63                      "ACCESS",
64                      "smartcrawler-access.log",
65                      ("" + lg.get("ACCESS")).equals("yes")
66                      );
67          }
68          if (lg.containsKey("LINK")) {
69              initLogger(
70                      new PatternLayout("[%d] - %m%n"),
71                      "LINK",
72                      "smartcrawler-linkbuilder.log",
73                      ("" + lg.get("LINK")).equals("yes")
74                      );
75          }
76          if (lg.containsKey("PERMISSIONS")) {
77              initLogger(
78                      new PatternLayout("[%d] - %m%n"),
79                      "PERMISSIONS",
80                      "smartcrawler-permissions.log",
81                      ("" + lg.get("PERMISSIONS")).equals("yes")
82                      );
83          }
84          if (lg.containsKey("EXTRACTOR")) {
85              initLogger(
86                      new PatternLayout("[%d] - %m%n"),
87                      "EXTRACTOR",
88                      "smartcrawler-extractor.log",
89                      ("" + lg.get("EXTRACTOR")).equals("yes")
90                      );
91          }
92          if (lg.containsKey("CONSOLE")) {
93              initLogger(
94                      new PatternLayout("[%d] - %m%n"),
95                      "CONSOLE",
96                      null,
97                      ("" + lg.get("CONSOLE")).equals("yes")
98                      );
99          }
100         if (lg.containsKey("PERSISTER")) {
101             initLogger(
102                     new PatternLayout("[%d] - %m%n"),
103                     "PERSISTER",
104                     "smartcrawler-persister.log",
105                     ("" + lg.get("PERSISTER")).equals("yes")
106                     );
107         }
108         if (lg.containsKey("PROVIDER")) {
109             initLogger(
110                     new PatternLayout("[%d] - %m%n"),
111                     "PROVIDER",
112                     "smartcrawler-provider.log",
113                     ("" + lg.get("PROVIDER")).equals("yes")
114                     );
115         }
116 
117     }
118 
119 
120     /***
121      *
122      * @param layout
123      * @param name
124      * @param fileName
125      * @param active
126      */
127     protected void initLogger(Layout layout, String name,
128             String fileName, boolean active) {
129 
130         Logger logger = null;
131         if (active) {
132             if (name == null) {
133                 logger = Logger.getRootLogger();
134                 logger.setLevel(Level.DEBUG);
135             } else {
136                 logger = Logger.getLogger(name);
137                 logger.setLevel(Level.INFO);
138             }
139             if ( fileName != null) {
140                 try {
141                     logger.addAppender(
142                             new RollingFileAppender(layout, fileName));
143 
144                 } catch(Exception e) {
145                     logger.addAppender(
146                             new ConsoleAppender(layout));
147                 }
148             } else { //if ( fileName != null) {
149                 logger.addAppender(
150                         new ConsoleAppender(layout));
151 
152             }
153             //logger.setLevel(Level.ALL);
154             logger.setAdditivity(false);
155         } else { //if (active) {
156             if (name == null) {
157                 logger = Logger.getRootLogger();
158                 logger.setLevel(Level.OFF);
159             } else {
160                 logger = Logger.getLogger(name);
161 
162                 logger.setLevel(Level.OFF);
163             }
164             logger.addAppender(
165                     new ConsoleAppender(layout));
166         }
167     }
168 
169     /***
170      *
171      * @param name
172      * @return
173      */
174     public Logger makeNewLoggerInstance(String name) {
175         return new SCLogger(name);
176     }
177 }
178