Line data Source code
1 : // Copyright (C) 2013 The Android Open Source Project 2 : // 3 : // Licensed under the Apache License, Version 2.0 (the "License"); 4 : // you may not use this file except in compliance with the License. 5 : // You may obtain a copy of the License at 6 : // 7 : // http://www.apache.org/licenses/LICENSE-2.0 8 : // 9 : // Unless required by applicable law or agreed to in writing, software 10 : // distributed under the License is distributed on an "AS IS" BASIS, 11 : // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 : // See the License for the specific language governing permissions and 13 : // limitations under the License. 14 : 15 : package com.google.gerrit.pgm.util; 16 : 17 : import java.util.logging.Formatter; 18 : import java.util.logging.Handler; 19 : import java.util.logging.Level; 20 : import java.util.logging.LogRecord; 21 : import java.util.logging.Logger; 22 : import java.util.logging.StreamHandler; 23 : 24 : public class GuiceLogger { 25 : private static final Handler HANDLER; 26 : 27 : static { 28 0 : HANDLER = 29 : new StreamHandler( 30 : System.out, 31 0 : new Formatter() { 32 : @Override 33 : public String format(LogRecord record) { 34 0 : return String.format( 35 0 : "[Guice %s] %s%n", record.getLevel().getName(), record.getMessage()); 36 : } 37 : }); 38 0 : HANDLER.setLevel(Level.ALL); 39 0 : } 40 : 41 : private GuiceLogger() {} 42 : 43 : public static Logger getLogger() { 44 0 : return Logger.getLogger("com.google.inject"); 45 : } 46 : 47 : public static void enable() { 48 0 : Logger guiceLogger = getLogger(); 49 0 : guiceLogger.addHandler(GuiceLogger.HANDLER); 50 0 : guiceLogger.setLevel(Level.ALL); 51 0 : } 52 : 53 : public static void disable() { 54 0 : Logger guiceLogger = getLogger(); 55 0 : guiceLogger.setLevel(Level.OFF); 56 0 : guiceLogger.removeHandler(GuiceLogger.HANDLER); 57 0 : } 58 : }