001/* 002 * flattr4j - A Java library for Flattr 003 * 004 * Copyright (C) 2012 Richard "Shred" Körber 005 * http://flattr4j.shredzone.org 006 * 007 * This program is free software: you can redistribute it and/or modify 008 * it under the terms of the GNU General Public License / GNU Lesser 009 * General Public License as published by the Free Software Foundation, 010 * either version 3 of the License, or (at your option) any later version. 011 * 012 * Licensed under the Apache License, Version 2.0 (the "License"); 013 * you may not use this file except in compliance with the License. 014 * 015 * This program is distributed in the hope that it will be useful, 016 * but WITHOUT ANY WARRANTY; without even the implied warranty of 017 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. 018 */ 019package org.shredzone.flattr4j.connector.impl; 020 021import java.text.MessageFormat; 022import java.util.logging.Level; 023 024import android.util.Log; 025 026/** 027 * A simple logger class that delegates log output either to {@link android.util.Log} (if 028 * available) or to {@link java.util.logging.Logger}. 029 * 030 * @author Richard "Shred" Körber 031 * @since 2.3 032 */ 033public class Logger { 034 private final java.util.logging.Logger logger; 035 private final String tag; 036 037 /** 038 * Creates a new {@link Logger}. 039 * 040 * @param tag 041 * Tag that is used for Android logging 042 * @param className 043 * Class name that is used for Java logging 044 */ 045 public Logger(String tag, String className) { 046 this.tag = tag; 047 048 java.util.logging.Logger log = null; 049 try { 050 // Test if Android logging is available... 051 Log.isLoggable(tag, Log.INFO); 052 } catch (Throwable t) { //NOSONAR: Ignore and use Java's logger 053 log = java.util.logging.Logger.getLogger(className); 054 } 055 this.logger = log; 056 } 057 058 /** 059 * Logs an exception on debug level. 060 * 061 * @param msg 062 * Message to be logged 063 * @param ex 064 * Exception to be logged 065 */ 066 public void debug(String msg, Throwable ex) { 067 if (logger != null) { 068 if (logger.isLoggable(Level.FINER)) { 069 logger.log(Level.FINER, msg, ex); 070 } 071 } else { 072 if (Log.isLoggable(tag, Log.DEBUG)) { 073 Log.v(tag, msg, ex); 074 } 075 } 076 } 077 078 /** 079 * Logs on a verbose level. 080 * 081 * @param msg 082 * Message to be logged 083 * @param args 084 * Arguments for formatting the message 085 */ 086 public void verbose(String msg, Object... args) { 087 if (logger != null) { 088 if (logger.isLoggable(Level.FINE)) { 089 logger.log(Level.FINE, msg, args); 090 } 091 } else { 092 if (Log.isLoggable(tag, Log.VERBOSE)) { 093 Log.v(tag, MessageFormat.format(msg, args)); 094 } 095 } 096 } 097 098 /** 099 * Logs on an info level. 100 * 101 * @param msg 102 * Message to be logged 103 * @param args 104 * Arguments for formatting the message 105 */ 106 public void info(String msg, Object... args) { 107 if (logger != null) { 108 if (logger.isLoggable(Level.INFO)) { 109 logger.log(Level.INFO, msg, args); 110 } 111 } else { 112 if (Log.isLoggable(tag, Log.INFO)) { 113 Log.i(tag, MessageFormat.format(msg, args)); 114 } 115 } 116 } 117 118 /** 119 * Logs on an error level. 120 * 121 * @param msg 122 * Message to be logged 123 * @param args 124 * Arguments for formatting the message 125 */ 126 public void error(String msg, Object... args) { 127 if (logger != null) { 128 if (logger.isLoggable(Level.WARNING)) { 129 logger.log(Level.WARNING, msg, args); 130 } 131 } else { 132 if (Log.isLoggable(tag, Log.WARN)) { 133 Log.w(tag, MessageFormat.format(msg, args)); 134 } 135 } 136 } 137 138}