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 */ 016package org.forgerock.i18n.jul; 017 018import java.util.Locale; 019import java.util.logging.Logger; 020 021/** 022 * A factory of {@link LocalizedLogger} instances which obtains a Java 023 * {@link Logger} by calling the appropriate {@link Logger} factory method and 024 * wrapping it in an instance of {@code LocalizedLogger}. 025 */ 026public final class LocalizedLoggerFactory { 027 /** 028 * Returns a localized logger factory which will create localized loggers 029 * for the default locale. 030 * 031 * @return The localized logger factory. 032 */ 033 public static LocalizedLoggerFactory getInstance() { 034 // This can't be cached because the default locale can change. 035 return new LocalizedLoggerFactory(Locale.getDefault()); 036 } 037 038 /** 039 * Returns a localized logger factory which will create localized loggers 040 * for the provided locale. 041 * 042 * @param locale 043 * The locale to which loggers created by the factory will 044 * localize all log messages. 045 * @return The localized logger factory. 046 */ 047 public static LocalizedLoggerFactory getInstance(final Locale locale) { 048 return new LocalizedLoggerFactory(locale); 049 } 050 051 private final Locale locale; 052 053 // Private constructor. 054 private LocalizedLoggerFactory(final Locale locale) { 055 this.locale = locale; 056 } 057 058 /** 059 * Returns a localized logger which will forward log messages to an 060 * anonymous Java {@code Logger} obtained by calling 061 * {@link Logger#getAnonymousLogger()} . 062 * 063 * @return The localized logger. 064 * @see Logger#getAnonymousLogger() 065 */ 066 public LocalizedLogger getLocalizedAnonymousLogger() { 067 final Logger logger = Logger.getAnonymousLogger(); 068 return new LocalizedLogger(logger, locale); 069 } 070 071 /** 072 * Returns a localized logger which will forward log messages to the 073 * provided Java {@code Logger}. 074 * 075 * @param logger 076 * The wrapped Java {@code Logger}. 077 * @return The localized logger. 078 */ 079 public LocalizedLogger getLocalizedLogger(final Logger logger) { 080 return new LocalizedLogger(logger, locale); 081 } 082 083 /** 084 * Returns a localized logger which will forward log messages to the named 085 * Java {@code Logger} obtained by calling {@link Logger#getLogger(String)}. 086 * 087 * @param name 088 * The name of the wrapped Java {@code Logger}. 089 * @return The localized logger. 090 * @see Logger#getLogger(String) 091 */ 092 public LocalizedLogger getLocalizedLogger(final String name) { 093 final Logger logger = Logger.getLogger(name); 094 return new LocalizedLogger(logger, locale); 095 } 096 097}