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  import org.apache.commons.lang3.StringUtils;
22  
23  /**
24   * A default implementation of Log that writes to System.out and System.err
25   */
26  public final class DefaultLog implements Log {
27      /**
28       * The environment variable to set the default log level that Rat should log at.
29       * If not specified INFO is used.
30       * @see #setLevel(Level)
31       */
32      public static final String ENV_VAR = DefaultLog.class.getName();
33  
34      /**
35       * The instance of the default log.
36       */
37      private static Log instance = new DefaultLog();
38  
39      /**
40       * Retrieves teh DefaultLog instance.
41       * @return the Default log instance.
42       */
43      public static Log getInstance() {
44          return instance;
45      }
46  
47      /**
48       * Sets the default log instance.
49       * If not set an instance of DefaultLog will be returned
50       * @param newInstance a Log to use as the default.
51       * @return the old instance.
52       */
53      public static Log setInstance(final Log newInstance) {
54          Log result = instance;
55          instance = newInstance == null ? new DefaultLog() : newInstance;
56          return result;
57      }
58  
59      /** The level at which we will write messages */
60      private Level level;
61  
62      private DefaultLog() {
63          try {
64              level = StringUtils.isNotEmpty(System.getenv(ENV_VAR)) ?
65                      Level.valueOf(System.getenv(ENV_VAR).toUpperCase()) : Level.INFO;
66          } catch (IllegalArgumentException e) {
67              level = Level.INFO;
68              log(Level.WARN, "Invalid log level set in environment", e);
69          }
70      }
71  
72      /**
73       * Sets the level. Log messages below the specified level will
74       * not be written to the log.
75       * @param level the level to use when writing messages.
76       */
77      public void setLevel(final Level level) {
78          this.level = level;
79      }
80  
81      /**
82       * Gets the level we are writing at.
83       * @return the level we are writing at.
84       */
85      public Level getLevel() {
86          return level;
87      }
88  
89      @Override
90      public void log(final Level level, final String msg) {
91          if (isEnabled(level)) {
92              switch (level) {
93                  case DEBUG:
94                  case INFO:
95                  case WARN:
96                      System.out.format("%s: %s%n", level, msg);
97                      break;
98                  case ERROR:
99                      System.err.format("%s: %s%n", level, msg);
100                     break;
101                 case OFF:
102                 default:
103                     break;
104             }
105         }
106     }
107 }