View Javadoc
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.rat.utils;
20  
21  /**
22   * A default implementation of Log that writes to System.out and System.err
23   */
24  public class DefaultLog implements Log {
25  
26      /**
27       * The instance of the default log.
28       */
29      private static Log INSTANCE = new DefaultLog();
30  
31      /**
32       * Retrieves teh DefaultLog instance.
33       * @return the Default log instance.
34       */
35      public static Log getInstance() {
36          return INSTANCE;
37      }
38  
39      /**
40       * Sets the default log instance.
41       * If not set an instance of DefaultLog will be returned
42       * @param instance a Log to use as the defult.
43       */
44      public static void setInstance(final Log instance) {
45          INSTANCE = instance == null ? new DefaultLog() : instance;
46      }
47      
48      private Level level;
49  
50      private DefaultLog() {
51          level = Level.WARN;
52      }
53  
54      public void setLevel(Level level) {
55          this.level = level;
56      }
57      
58      public Level getLevel() {
59          return level;
60      }
61      
62      @Override
63      public void log(Level level, String msg) {
64          if (this.level.ordinal() <= level.ordinal())
65              switch (level) {
66              case DEBUG:
67              case INFO:
68              case WARN:
69                  System.out.format("%s: %s%n", level, msg);
70                  break;
71              case ERROR:
72                  System.err.format("%s: %s%n", level, msg);
73                  break;
74              case OFF:
75                  break;
76              default:
77                  break;
78              }
79      }
80  }