001/* 002 * The contents of this file are subject to the terms of the Common Development and 003 * Distribution License (the License). You may not use this file except in compliance with the 004 * License. 005 * 006 * You can obtain a copy of the License at legal/CDDLv1.0.txt. See the License for the 007 * specific language governing permission and limitations under the License. 008 * 009 * When distributing Covered Software, include this CDDL Header Notice in each file and include 010 * the License file at legal/CDDLv1.0.txt. If applicable, add the following below the CDDL 011 * Header, with the fields enclosed by brackets [] replaced by your own identifying 012 * information: "Portions Copyrighted [year] [name of copyright owner]". 013 * 014 * Copyright 2011 ForgeRock AS 015 */ 016 017package org.forgerock.i18n.slf4j; 018 019import java.util.Locale; 020 021import org.slf4j.Logger; 022import org.slf4j.LoggerFactory; 023 024/** 025 * A factory of {@link LocalizedLogger} instances which obtains a SLF4J 026 * {@link Logger} by calling the appropriate {@link LoggerFactory} method and 027 * wrapping it in an instance of {@code LocalizedLogger}. 028 */ 029public final class LocalizedLoggerFactory { 030 /** 031 * Returns a localized logger factory which will create localized loggers 032 * for the default locale. 033 * 034 * @return The localized logger factory. 035 */ 036 public static LocalizedLoggerFactory getInstance() { 037 return new LocalizedLoggerFactory(Locale.getDefault()); 038 } 039 040 /** 041 * Returns a localized logger factory which will create localized loggers 042 * for the provided locale. 043 * 044 * @param locale 045 * The locale to which loggers created by the factory will 046 * localize all log messages. 047 * @return The localized logger factory. 048 */ 049 public static LocalizedLoggerFactory getInstance(final Locale locale) { 050 return new LocalizedLoggerFactory(locale); 051 } 052 053 private final Locale locale; 054 055 // Private constructor. 056 private LocalizedLoggerFactory(final Locale locale) { 057 this.locale = locale; 058 } 059 060 /** 061 * Returns a localized logger which will forward log messages to an SLF4J 062 * {@code Logger} obtained by calling {@link LoggerFactory#getLogger(Class)} 063 * . 064 * 065 * @param clazz 066 * The name of the wrapped SLF4J {@code Logger}. 067 * @return The localized logger. 068 * @see LoggerFactory#getLogger(Class) 069 */ 070 public LocalizedLogger getLocalizedLogger(final Class<?> clazz) { 071 final Logger logger = LoggerFactory.getLogger(clazz); 072 return new LocalizedLogger(logger, locale); 073 } 074 075 /** 076 * Returns a localized logger which will forward log messages to the 077 * provided SLF4J {@code Logger}. 078 * 079 * @param logger 080 * The wrapped SLF4J {@code Logger}. 081 * @return The localized logger. 082 * @see LoggerFactory#getLogger(String) 083 */ 084 public LocalizedLogger getLocalizedLogger(final Logger logger) { 085 return new LocalizedLogger(logger, locale); 086 } 087 088 /** 089 * Returns a localized logger which will forward log messages to an SLF4J 090 * {@code Logger} obtained by calling 091 * {@link LoggerFactory#getLogger(String)}. 092 * 093 * @param name 094 * The name of the wrapped SLF4J {@code Logger}. 095 * @return The localized logger. 096 * @see LoggerFactory#getLogger(String) 097 */ 098 public LocalizedLogger getLocalizedLogger(final String name) { 099 final Logger logger = LoggerFactory.getLogger(name); 100 return new LocalizedLogger(logger, locale); 101 } 102 103}