Skip to content
This repository

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
tree: bf0c89fbd7
Fetching contributors…

Cannot retrieve contributors at this time

file 312 lines (242 sloc) 9.204 kb
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312
/*
* Copyright 2004-2005 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.codehaus.groovy.grails.plugins.logging

import grails.util.Environment

import org.apache.log4j.Appender
import org.apache.log4j.ConsoleAppender
import org.apache.log4j.Level
import org.apache.log4j.LogManager
import org.apache.log4j.Logger
import org.apache.log4j.WriterAppender
import org.codehaus.groovy.grails.plugins.log4j.Log4jConfig

class Log4jDslTests extends GroovyTestCase {

    private Log4jConfig log4jConfig = new Log4jConfig(new ConfigObject())

    protected void setUp() {
        super.setUp()
        System.setProperty(Environment.KEY, "")
    }

    protected void tearDown() {
        System.setProperty(Environment.KEY, "")
    }

    void testSingleDebugStatement() {
        LogManager.resetConfiguration()

        log4jConfig.configure {
            debug 'org.hibernate.SQL'
        }
        def hibernateLogger = Logger.getLogger("org.hibernate.SQL")

        assertEquals hibernateLogger.level, Level.DEBUG
    }

    void testEnvironmentSpecificLogging() {

        System.setProperty(Environment.KEY, "production")

        LogManager.resetConfiguration()

        log4jConfig.configure {
            environments {
                development {
                    trace 'org.hibernate.SQL'
                }
                production {
                    error 'org.hibernate.SQL'
                }
                firstCustomEnv {
                    warn 'org.hibernate.SQL'
                }
                secondCustomEnv {
                    debug 'org.hibernate.SQL'
                }
            }
        }

        def hibernateLogger = Logger.getLogger("org.hibernate.SQL")
        assertEquals hibernateLogger.level, Level.ERROR
    }

    void testCustomEnvironment() {

        System.setProperty(Environment.KEY, "firstCustomEnv")

        LogManager.resetConfiguration()

        Log4jConfig config = new Log4jConfig()
        config.configure {
            environments {
                development {
                    trace 'org.hibernate.SQL'
                }
                production {
                    error 'org.hibernate.SQL'
                }
                firstCustomEnv {
                    warn 'org.hibernate.SQL'
                }
                secondCustomEnv {
                    debug 'org.hibernate.SQL'
                }
            }
        }

        def hibernateLogger = Logger.getLogger("org.hibernate.SQL")
        assertEquals hibernateLogger.level, Level.WARN

    }

    void testTraceLevel() {
        LogManager.resetConfiguration()

        log4jConfig.configure {
            trace 'org.hibernate.SQL'
        }

        def hibernateLogger = Logger.getLogger("org.hibernate.SQL")
        assertEquals hibernateLogger.level, Level.TRACE
    }

    void testConfigureRootLogger() {
        LogManager.resetConfiguration()

        log4jConfig.configure {
            root {
                debug()
                additivity = true
            }
        }

        def r = Logger.getRootLogger()

        assertEquals Level.DEBUG, r.level
        assertTrue r.additivity
        Appender a = r.allAppenders.nextElement()
        assertEquals "stdout",a.name

        LogManager.resetConfiguration()

        log4jConfig.configure {
            appenders {
                appender new WriterAppender(name:'writerAppender')
            }
            root {
                trace 'writerAppender'
                additivity = true
            }
        }

        r = Logger.getRootLogger()

        assertEquals Level.TRACE, r.level
        assertTrue r.additivity
        a = r.allAppenders.nextElement()
        assertEquals "writerAppender",a.name

        LogManager.resetConfiguration()

        log4jConfig.configure {
            appenders {
                appender new WriterAppender(name:'writerAppender')
            }
            root {
                warn 'writerAppender', 'stdout'
                additivity = true
            }
        }

        r = Logger.getRootLogger()

        assertEquals Level.WARN, r.level
        assertTrue r.additivity
        Enumeration appenders = r.allAppenders
        a = appenders.nextElement()
        assertEquals "writerAppender",a.name
        a = appenders.nextElement()
        assertEquals "stdout",a.name
    }

    void testSensibleDefaults() {
        LogManager.resetConfiguration()

        log4jConfig.configure {
            debug 'org.codehaus.groovy.grails.web.servlet',
                  'org.codehaus.groovy.grails.web.pages'

            error 'org.codehaus.groovy.grails.web.sitemesh'
        }

        def root = Logger.getRootLogger()
        assertEquals Level.ERROR, root.level

        def appenders = root.getAllAppenders()
        assertTrue appenders.hasMoreElements()

        def consoleAppender = appenders.nextElement()
        assertEquals "stdout", consoleAppender.name

        def stackLogger = Logger.getLogger("StackTrace")
        assertFalse stackLogger.additivity

        assertTrue stackLogger.allAppenders.hasMoreElements()
        def fileAppender = stackLogger.allAppenders.nextElement()

        assertEquals "stacktrace.log", fileAppender.file

        def logger = Logger.getLogger('org.codehaus.groovy.grails.web.servlet')
        assertEquals Level.DEBUG, logger.level
    }

    void testCustomAppender() {

        LogManager.resetConfiguration()

        def consoleAppender
        log4jConfig.configure {
            appenders {
                console follow: true, name: 'customAppender', layout: pattern(conversionPattern: '%c{2} %m%n')
                appender name:'writerAppender', new WriterAppender()
            }
            debug customAppender: ['org.codehaus.groovy.grails.web.servlet',
                                   'org.codehaus.groovy.grails.web.pages']

            error customAppender: 'org.codehaus.groovy.grails.web.sitemesh',
                  writerAppender: 'org.codehaus.groovy.grails.web.sitemesh'
        }

        def logger = Logger.getLogger('org.codehaus.groovy.grails.web.servlet')
        assertEquals Level.DEBUG, logger.level
        def appender = logger.getAppender("customAppender")

        assert appender
        assertTrue appender.follow
        assertEquals '%c{2} %m%n', appender.layout.conversionPattern

        logger = Logger.getLogger('org.codehaus.groovy.grails.web.sitemesh')

        assertEquals Level.ERROR, logger.level

        appender = logger.getAppender("customAppender")
        assert appender
        assertEquals '%c{2} %m%n', appender.layout.conversionPattern
    }

    void testCustomAppenderWithInstance() {
        LogManager.resetConfiguration()

        log4jConfig.configure {
            appenders {
                def consoleAppender = new ConsoleAppender(follow: true, target: "System.out",
                    layout: pattern(conversionPattern: '%c{2} %m%n'))
                appender name:'customAppender', consoleAppender
            }

            error customAppender:'org.codehaus.groovy.grails.web.sitemesh'
        }

        def logger = Logger.getLogger('org.codehaus.groovy.grails.web.sitemesh')

        assertEquals Level.ERROR, logger.level
        def appender = logger.getAppender("customAppender")

        assert appender
        assertTrue appender.follow
        assertEquals "System.out", appender.target
        assertEquals '%c{2} %m%n', appender.layout.conversionPattern
    }

    /**
* Tests that you can configure the root loader via the argument
* passed into the Log4J closure.
*/
    void testRootLoggerModification() {

        LogManager.resetConfiguration()

        log4jConfig.configure { root ->
            root.level = Level.DEBUG
        }

        def rootLogger = Logger.rootLogger
        assertEquals Level.DEBUG, rootLogger.level
    }

    void testConfigFromCollection() {
        LogManager.resetConfiguration()

        log4jConfig.configure([{
            debug 'org.hibernate.SQL'
        },
        {
            warn 'org.hibernate.SQL'
        }])
        def hibernateLogger = Logger.getLogger("org.hibernate.SQL")

        assertEquals Level.WARN, hibernateLogger.level
    }

    void testConfigFromMap() {
        LogManager.resetConfiguration()

        def configData = [:]
        configData.main = {
            debug 'org.hibernate.SQL'
        }
        configData.secondary = {
            warn 'org.hibernate.SQL'
        }
        log4jConfig.configure(configData)

        def hibernateLogger = Logger.getLogger("org.hibernate.SQL")
        assertEquals Level.WARN, hibernateLogger.level
    }
}
Something went wrong with that request. Please try again.