Start line:  
End line:  

Snippet Preview

Snippet HTML Code

Stack Overflow Questions
Licensed to the Apache Software Foundation (ASF) under one or more contributor license agreements. See the NOTICE file distributed with this work for additional information regarding copyright ownership. The ASF licenses this file to you 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.apache.cxf.common.logging;
 
 
A container for static utility methods related to logging. By default, CXF logs to java.util.logging. An application can change this. To log to another system, the application must provide an object that extends AbstractDelegatingLogger, and advertise that class via one of the following mechanisms:
  • Create a file, in the classpath, named META-INF/cxf/org.apache.cxf.Logger. This file should contain the fully-qualified name of the class, with no comments, on a single line.
  • Call setLoggerClass(java.lang.Class) with a Class<?> reference to the logger class.
CXF provides Log4jLogger to use log4j instead of java.util.logging.
 
 public final class LogUtils {
     public static final String KEY = "org.apache.cxf.Logger";
     
     private static final Object[] NO_PARAMETERS = new Object[0];
 
     
     private static Class<?> loggerClass;
    
    
Prevents instantiation.
 
     private LogUtils() {
     }
 
     static {
         try {
             String cname = System.getProperty();
             if (StringUtils.isEmpty(cname)) {
                 InputStream ins = Thread.currentThread().getContextClassLoader()
                     .getResourceAsStream("META-INF/cxf/" + );
                 if (ins == null) {
                     ins = ClassLoader.getSystemResourceAsStream("META-INF/cxf/" + );
                 }
                 if (ins != null) {
                     BufferedReader din = new BufferedReader(new InputStreamReader(ins));
                     cname = din.readLine();
                 }
             }
             if (!StringUtils.isEmpty(cname)) {
                  = Class.forName(cnametrue,
                                             Thread.currentThread().getContextClassLoader());
                 getLogger(LogUtils.class).fine("Using " + .getName() + " for logging.");
             }
         } catch (Exception ex) {
             //ignore
         }
     }
    
    
    
Specify a logger class that inherits from AbstractDelegatingLogger. Enable users to use their own logger implementation.
 
     public static void setLoggerClass(Class<? extends AbstractDelegatingLoggercls) {
          = cls;
     }

    
    
Get a Logger with the associated default resource bundle for the class.

Parameters:
cls the Class to contain the Logger
Returns:
an appropriate Logger
    public static Logger getLogger(Class<?> cls) {
        return createLogger(clsnullcls.getName());
    }
    
    
Get a Logger with an associated resource bundle.

Parameters:
cls the Class to contain the Logger
name the resource name
Returns:
an appropriate Logger
    public static Logger getLogger(Class<?> clsString resourcename) {
        return createLogger(clsresourcenamecls.getName());
    }

    
Get a Logger with an associated resource bundle.

Parameters:
cls the Class to contain the Logger (to find resources)
name the resource name
loggerName the full name for the logger
Returns:
an appropriate Logger
    public static Logger getLogger(Class<?> cls,
                                     String resourcename,
                                     String loggerName) {
        return createLogger(clsresourcenameloggerName);
    }

    
Get a Logger with the associated default resource bundle for the class.

Parameters:
cls the Class to contain the Logger
Returns:
an appropriate Logger
    public static Logger getL7dLogger(Class<?> cls) {
        return createLogger(clsnullcls.getName());
    }
    
    
Get a Logger with an associated resource bundle.

Parameters:
cls the Class to contain the Logger
name the resource name
Returns:
an appropriate Logger
    public static Logger getL7dLogger(Class<?> clsString resourcename) {
        return createLogger(clsresourcenamecls.getName());
    }

    
Get a Logger with an associated resource bundle.

Parameters:
cls the Class to contain the Logger (to find resources)
name the resource name
loggerName the full name for the logger
Returns:
an appropriate Logger
    public static Logger getL7dLogger(Class<?> cls,
                                      String resourcename,
                                      String loggerName) {
        return createLogger(clsresourcenameloggerName);
    }
    
    
Create a logger
    protected static Logger createLogger(Class<?> cls
                                         String name
                                         String loggerName) {
        if ( != null) {
            try {
                Constructor cns = .getConstructor(String.classString.class);
                if (name == null) {
                    try {
                        return (Loggercns.newInstance(loggerName, BundleUtils.getBundleName(cls));
                    } catch (InvocationTargetException ite) {
                        if (ite.getTargetException() instanceof MissingResourceException) {
                            return (Loggercns.newInstance(loggerNamenull);
                        } else {
                            throw ite;
                        }
                    } 
                } else {
                    try {
                        return (Loggercns.newInstance(loggerName, BundleUtils.getBundleName(clsname));
                    } catch (InvocationTargetException ite) {
                        if (ite.getTargetException() instanceof MissingResourceException) {
                            throw (MissingResourceException)ite.getTargetException();
                        } else {
                            throw ite;
                        }
                    } 
                }
            } catch (Exception e) {
                throw new RuntimeException(e);
            }
        }
        if (name == null) {
            try {
                return Logger.getLogger(loggerName, BundleUtils.getBundleName(cls)); //NOPMD
            } catch (MissingResourceException rex) {
                return Logger.getLogger(loggerNamenull); //NOPMD
            }
        } else {
            return Logger.getLogger(loggerName, BundleUtils.getBundleName(clsname)); //NOPMD
        }
    }

    
Allows both parameter substitution and a typed Throwable to be logged.

Parameters:
logger the Logger the log to
level the severity level
message the log message
throwable the Throwable to log
parameter the parameter to substitute into message
    public static void log(Logger logger
                           Level level
                           String message
                           Throwable throwable,
                           Object parameter) {
        if (logger.isLoggable(level)) {
            final String formattedMessage = 
                MessageFormat.format(localize(loggermessage), parameter);
            doLog(loggerlevelformattedMessagethrowable);
        }
    }

    
Allows both parameter substitution and a typed Throwable to be logged.

Parameters:
logger the Logger the log to
level the severity level
message the log message
throwable the Throwable to log
parameters the parameters to substitute into message
    public static void log(Logger logger
                           Level level
                           String message
                           Throwable throwable,
                           Object... parameters) {
        if (logger.isLoggable(level)) {
            final String formattedMessage = 
                MessageFormat.format(localize(loggermessage), parameters);
            doLog(loggerlevelformattedMessagethrowable);
        }
    }
 
    
Checks log level and logs

Parameters:
logger the Logger the log to
level the severity level
message the log message
    
    public static void log(Logger logger
                           Level level
                           String message) {
        log(loggerlevelmessage);        
    }  
    
    
Checks log level and logs

Parameters:
logger the Logger the log to
level the severity level
message the log message
throwable the Throwable to log
      
    public static void log(Logger logger
                           Level level
                           String message
                           Throwable throwable) {
        log(loggerlevelmessagethrowable);
    }
  
    
Checks log level and logs

Parameters:
logger the Logger the log to
level the severity level
message the log message
parameter the parameter to substitute into message
      
    public static void log(Logger logger
                           Level level
                           String message
                           Object parameter) {
        log(loggerlevelmessagenew Object[] {parameter});
    }
    
    
Checks log level and logs

Parameters:
logger the Logger the log to
level the severity level
message the log message
parameters the parameters to substitute into message
      
    public static void log(Logger logger
                           Level level
                           String message
                           Object[] parameters) {
        if (logger.isLoggable(level)) {
            String msg = localize(loggermessage);
            try {
                msg = MessageFormat.format(msgparameters);
            } catch (IllegalArgumentException ex) {
                //ignore, log as is
            }
            doLog(loggerlevelmsgnull);
        }        
    }
    private static void doLog(Logger logLevel levelString msgThrowable t) {
        LogRecord record = new LogRecord(levelmsg);
    
        record.setLoggerName(log.getName());
        record.setResourceBundleName(log.getResourceBundleName());
        record.setResourceBundle(log.getResourceBundle());
            
        if (t != null) {
            record.setThrown(t);
        }
        
        //try to get the right class name/method name - just trace
        //back the stack till we get out of this class
        StackTraceElement stack[] = (new Throwable()).getStackTrace();
        String cname = LogUtils.class.getName();
        for (int x = 0; x < stack.lengthx++) {
            StackTraceElement frame = stack[x];
            if (!frame.getClassName().equals(cname)) {
                record.setSourceClassName(frame.getClassName());
                record.setSourceMethodName(frame.getMethodName());
                break;
            }
        }
        log.log(record);
    }

    
Retrieve localized message retrieved from a logger's resource bundle.

Parameters:
logger the Logger
message the message to be localized
    private static String localize(Logger loggerString message) {
        ResourceBundle bundle = logger.getResourceBundle();
        try {
            return bundle != null ? bundle.getString(message) : message;
        } catch (MissingResourceException ex) {
            //string not in the bundle
            return message;
        }
    }
New to GrepCode? Check out our FAQ X