##### Coverage Report - org.owasp.dependencycheck.reporting.VelocityLoggerRedirect

Classes in this File | Line Coverage | Branch Coverage | Complexity
VelocityLoggerRedirect |
57% | 8/14
33% | 2/6
| 3.0;3

1 |  |
     /*
2 |  |
      * This file is part of dependency-check-core.
3 |  |
      *
4 |  |
      * Dependency-check-core is free software: you can redistribute it and/or modify it
5 |  |
      * under the terms of the GNU General Public License as published by the Free
6 |  |
      * Software Foundation, either version 3 of the License, or (at your option) any
7 |  |
      * later version.
8 |  |
      *
9 |  |
      * Dependency-check-core is distributed in the hope that it will be useful, but
10 |  |
      * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
11 |  |
      * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more
12 |  |
      * details.
13 |  |
      *
14 |  |
      * You should have received a copy of the GNU General Public License along with
15 |  |
      * dependency-check-core. If not, see http://www.gnu.org/licenses/.
16 |  |
      *
17 |  |
      * Copyright (c) 2013 Steve Springett. All Rights Reserved.
18 |  |
      */
19 |  |
     package org.owasp.dependencycheck.reporting;
20 |  |
21 |  |
     import org.apache.velocity.app.Velocity;
22 |  |
     import org.apache.velocity.runtime.RuntimeServices;
23 |  |
     import org.apache.velocity.runtime.log.LogChute;
24 |  |
25 |  |
     import java.util.logging.Level;
26 |  |
     import java.util.logging.Logger;
27 |  |
28 |  |
     /**
29 |  |
      * <p>DependencyCheck uses {@link java.util.logging.Logger} as a logging
30 |  |
      * framework, and Apache Velocity uses a custom logging implementation that
31 |  |
      * outputs to a file named velocity.log by default. This class is an
32 |  |
      * implementation of a custom Velocity logger that redirects all velocity
33 |  |
      * logging to the Java Logger class.
34 |  |
      * </p><p>
35 |  |
      * This class was written to address permission issues when using
36 |  |
      * Dependency-Check in a server environment (such as the Jenkins plugin). In
37 |  |
      * some circumstances, Velocity would attempt to create velocity.log in an
38 |  |
      * un-writable directory.</p>
39 |  |
      *
40 |  |
      * @author Steve Springett (steve.springett@owasp.org)
41 |  |
      */
42 | 1 |
     public class VelocityLoggerRedirect implements LogChute {
43 |  |
44 |  |
         /**
45 |  |
          * This will be invoked once by the LogManager.
46 |  |
          *
47 |  |
          * @param rsvc the RuntimeServices
48 |  |
          */
49 |  |
         public void init(RuntimeServices rsvc) {
50 |  |
             // do nothing
51 | 1 |
         }
52 |  |
53 |  |
         /**
54 |  |
          * Given a Velocity log level and message, this method will call the
55 |  |
          * appropriate Logger level and log the specified values.
56 |  |
          *
57 |  |
          * @param level the logging level
58 |  |
          * @param message the message to be logged
59 |  |
          */
60 |  |
         public void log(int level, String message) {
61 | 32 |
             Logger.getLogger(Velocity.class.getName()).log(getLevel(level), message);
62 | 32 |
         }
63 |  |
64 |  |
         /**
65 |  |
          * Given a Velocity log level, message and Throwable, this method will call
66 |  |
          * the appropriate Logger level and log the specified values.
67 |  |
          *
68 |  |
          * @param level the logging level
69 |  |
          * @param message the message to be logged
70 |  |
          * @param t a throwable to log
71 |  |
          */
72 |  |
         public void log(int level, String message, Throwable t) {
73 | 0 |
             Logger.getLogger(Velocity.class.getName()).log(getLevel(level), message, t);
74 | 0 |
         }
75 |  |
76 |  |
         /**
77 |  |
          * Will always return true. The property file will decide what level to log.
78 |  |
          *
79 |  |
          * @param level the logging level
80 |  |
          * @return true
81 |  |
          */
82 |  |
         public boolean isLevelEnabled(int level) {
83 | 13 |
             return true;
84 |  |
         }
85 |  |
86 |  |
         /**
87 |  |
          * Maps Velocity log levels to {@link Logger} values.
88 |  |
          *
89 |  |
          * @param velocityLevel the logging level
90 |  |
          * @return the logging level
91 |  |
          */
92 |  |
         private Level getLevel(int velocityLevel) {
93 | 32 |
             switch (velocityLevel) {
94 |  |
                 case TRACE_ID:
95 | 8 |
                     return Level.ALL;
96 |  |
                 case DEBUG_ID:
97 | 24 |
                     return Level.FINE;
98 |  |
                 case INFO_ID:
99 | 0 |
                     return Level.INFO;
100 |  |
                 case WARN_ID:
101 | 0 |
                     return Level.WARNING;
102 |  |
                 case ERROR_ID:
103 | 0 |
                     return Level.SEVERE;
104 |  |
                 default:
105 | 0 |
                     return Level.INFO;
106 |  |
             }
107 |  |
         }
108 |  |
     }