Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import org.apache.log4j.LogManager
- import org.apache.log4j.PropertyConfigurator
- /**
- * This class will inject Log4j logger to your classes during integration test
- * Please put a log4j_test.properties file with your test config in src/java
- * directory or change the file loader to what suits you best.
- */
- class TestLoggerLoader {
- /**
- * Default list of classes which will have logging injected
- * Can be externalised if needed
- */
- private static final List<String> classes = [
- "com.example.loggingTest.SrcTest",
- "grailsloggingtests.Test",
- "grailsloggingtests.TestController",
- "grailsloggingtests.TestService"
- ]
- /**
- * Inject logging for single class
- * @param fullClassName i.e. com.example.myProject.MyClass
- */
- public static void addLogging(String fullClassName) {
- List<String> classList = [fullClassName]
- loadLogging(classList)
- }
- /**
- * Inject logging for classes defined in 'classes' list
- */
- public static void addLogging() {
- loadLogging(classes)
- }
- /**
- * Inject logging for a list of classes
- * @param newClasses - List<String> of full class names
- */
- public static void addLogging(List<String> newClasses) {
- loadLogging(newClasses)
- }
- private static void loadLogging(List<String> classes) {
- Properties props = new Properties();
- try {
- InputStream configStream = Thread.currentThread().contextClassLoader.getResourceAsStream("log4j_test.properties")
- props.load(configStream)
- configStream.close()
- LogManager.resetConfiguration();
- PropertyConfigurator.configure(props);
- for (className in classes) {
- def clazz = Class.forName(className, true, Thread.currentThread().contextClassLoader).newInstance()
- def logger = LogManager.getLogger(clazz.class.simpleName)
- clazz.class.metaClass.getLog << {-> logger}
- }
- } catch (Exception e) {
- System.out.println("Error: Cannot inject logging");
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement