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 {@code System.out} and {@code 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      /**
60       * Creates a new instance of the default log.
61       * @return A new instance of the default log.
62       */
63      public static Log createDefault() {
64          return new DefaultLog();
65      }
66  
67      /** The level at which we will write messages */
68      private Level level;
69  
70      private DefaultLog() {
71          try {
72              level = StringUtils.isNotEmpty(System.getenv(ENV_VAR)) ?
73                      Level.valueOf(System.getenv(ENV_VAR).toUpperCase()) : Level.INFO;
74          } catch (IllegalArgumentException e) {
75              level = Level.INFO;
76              log(Level.WARN, "Invalid log level set in environment", e);
77          }
78      }
79  
80      /**
81       * Sets the level. Log messages below the specified level will
82       * not be written to the log.
83       * @param level the level to use when writing messages.
84       */
85      public void setLevel(final Level level) {
86          this.level = level;
87      }
88  
89      /**
90       * Gets the level we are writing at.
91       * @return the level we are writing at.
92       */
93      public Level getLevel() {
94          return level;
95      }
96  
97      @Override
98      public void log(final Level level, final String msg) {
99          if (isEnabled(level)) {
100             switch (level) {
101                 case DEBUG:
102                 case INFO:
103                 case WARN:
104                     System.out.format("%s: %s%n", level, msg);
105                     break;
106                 case ERROR:
107                     System.err.format("%s: %s%n", level, msg);
108                     break;
109                 case OFF:
110                 default:
111                     break;
112             }
113         }
114     }
115 }