KEYCLOAK-1280: I18N for logging

This commit is contained in:
Stan Silvert 2016-01-15 15:29:45 -05:00
parent 0df651cc0c
commit 7514104974
4 changed files with 132 additions and 17 deletions

13
pom.xml Executable file → Normal file
View file

@ -35,7 +35,8 @@
<undertow.version>1.1.1.Final</undertow.version> <undertow.version>1.1.1.Final</undertow.version>
<picketlink.version>2.7.0.Final</picketlink.version> <picketlink.version>2.7.0.Final</picketlink.version>
<mongo.driver.version>3.2.0</mongo.driver.version> <mongo.driver.version>3.2.0</mongo.driver.version>
<jboss.logging.version>3.1.4.GA</jboss.logging.version> <jboss.logging.version>3.3.0.Final</jboss.logging.version>
<jboss.logging.tools.version>2.0.1.Final</jboss.logging.tools.version>
<jboss-logging-tools.version>1.2.0.Beta1</jboss-logging-tools.version> <jboss-logging-tools.version>1.2.0.Beta1</jboss-logging-tools.version>
<jboss.spec.javax.xml.bind.jboss-jaxb-api_2.2_spec.version>1.0.4.Final</jboss.spec.javax.xml.bind.jboss-jaxb-api_2.2_spec.version> <jboss.spec.javax.xml.bind.jboss-jaxb-api_2.2_spec.version>1.0.4.Final</jboss.spec.javax.xml.bind.jboss-jaxb-api_2.2_spec.version>
<jboss-jaxrs-api_2.0_spec>1.0.0.Final</jboss-jaxrs-api_2.0_spec> <jboss-jaxrs-api_2.0_spec>1.0.0.Final</jboss-jaxrs-api_2.0_spec>
@ -297,6 +298,16 @@
<artifactId>jboss-logging</artifactId> <artifactId>jboss-logging</artifactId>
<version>${jboss.logging.version}</version> <version>${jboss.logging.version}</version>
</dependency> </dependency>
<dependency>
<groupId>org.jboss.logging</groupId>
<artifactId>jboss-logging-annotations</artifactId>
<version>${jboss.logging.tools.version}</version>
</dependency>
<dependency>
<groupId>org.jboss.logging</groupId>
<artifactId>jboss-logging-processor</artifactId>
<version>${jboss.logging.tools.version}</version>
</dependency>
<dependency> <dependency>
<groupId>log4j</groupId> <groupId>log4j</groupId>
<artifactId>log4j</artifactId> <artifactId>log4j</artifactId>

View file

@ -57,6 +57,18 @@
<groupId>org.jboss.logging</groupId> <groupId>org.jboss.logging</groupId>
<artifactId>jboss-logging</artifactId> <artifactId>jboss-logging</artifactId>
</dependency> </dependency>
<dependency>
<groupId>org.jboss.logging</groupId>
<artifactId>jboss-logging-annotations</artifactId>
<scope>provided</scope>
<optional>true</optional>
</dependency>
<dependency>
<groupId>org.jboss.logging</groupId>
<artifactId>jboss-logging-processor</artifactId>
<scope>provided</scope>
<optional>true</optional>
</dependency>
<dependency> <dependency>
<groupId>org.jboss.resteasy</groupId> <groupId>org.jboss.resteasy</groupId>
<artifactId>resteasy-jaxrs</artifactId> <artifactId>resteasy-jaxrs</artifactId>
@ -129,6 +141,9 @@
<configuration> <configuration>
<source>${maven.compiler.source}</source> <source>${maven.compiler.source}</source>
<target>${maven.compiler.target}</target> <target>${maven.compiler.target}</target>
<compilerArgument>
-AgeneratedTranslationFilesPath=${project.build.directory}/generated-translation-files
</compilerArgument>
</configuration> </configuration>
</plugin> </plugin>
</plugins> </plugins>

View file

@ -0,0 +1,89 @@
/*
* Copyright 2016 Red Hat Inc. and/or its affiliates and other contributors
* as indicated by the @author tags. All rights reserved.
*
* Licensed 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.keycloak.services;
import org.jboss.logging.BasicLogger;
import org.jboss.logging.Logger;
import org.jboss.logging.annotations.Cause;
import org.jboss.logging.annotations.LogMessage;
import org.jboss.logging.annotations.Message;
import org.jboss.logging.annotations.MessageLogger;
import static org.jboss.logging.Logger.Level.ERROR;
import static org.jboss.logging.Logger.Level.INFO;
import static org.jboss.logging.Logger.Level.WARN;
/**
* Main logger for the Keycloak Services module.
*
* @author Stan Silvert ssilvert@redhat.com (C) 2016 Red Hat Inc.
*/
@MessageLogger(projectCode="KC-SERVICES", length=4)
public interface ServicesLogger extends BasicLogger {
ServicesLogger ROOT_LOGGER = Logger.getMessageLogger(ServicesLogger.class, "org.keycloak.services");
@LogMessage(level = INFO)
@Message(id=1, value="Loading config from %s")
void loadingFrom(Object from);
@LogMessage(level = ERROR)
@Message(id=2, value="Failed to migrate datamodel")
void migrationFailure(@Cause Throwable t);
@LogMessage(level = INFO)
@Message(id=3, value="Not importing realm %s from %s. It already exists.")
void realmExists(String realmName, String from);
@LogMessage(level = INFO)
@Message(id=4, value="Imported realm %s from %s.")
void importedRealm(String realmName, String from);
@LogMessage(level = WARN)
@Message(id=5, value="Unable to import realm %s from %s.")
void unableToImportRealm(@Cause Throwable t, String realmName, String from);
@LogMessage(level = INFO)
@Message(id=6, value="Importing users from '%s'")
void imprtingUsersFrom(Object from);
@LogMessage(level = ERROR)
@Message(id=7, value="Failed to load 'keycloak-add-user.json'")
void failedToLoadUsers(@Cause Throwable t);
@LogMessage(level = ERROR)
@Message(id=8, value="Failed to add user %s to realm %s: realm not found")
void addUserFailedRealmNotFound(String user, String realm);
@LogMessage(level = INFO)
@Message(id=9, value="Added user '%s' to realm '%s'")
void addUserSuccess(String user, String realm);
@LogMessage(level = ERROR)
@Message(id=10, value="Failed to add user '%s' to realm '%s': user with username exists")
void addUserFailedUserExists(String user, String realm);
@LogMessage(level = ERROR)
@Message(id=11, value="Failed to add user '%s' to realm '%s'")
void addUserFailed(@Cause Throwable t, String user, String realm);
@LogMessage(level = ERROR)
@Message(id=12, value="Failed to delete '%s'")
void failedToDeleteFile(String fileName);
}

View file

@ -3,7 +3,6 @@ package org.keycloak.services.resources;
import com.fasterxml.jackson.core.type.TypeReference; import com.fasterxml.jackson.core.type.TypeReference;
import com.fasterxml.jackson.databind.JsonNode; import com.fasterxml.jackson.databind.JsonNode;
import com.fasterxml.jackson.databind.ObjectMapper; import com.fasterxml.jackson.databind.ObjectMapper;
import org.jboss.logging.Logger;
import org.jboss.resteasy.core.Dispatcher; import org.jboss.resteasy.core.Dispatcher;
import org.jboss.resteasy.spi.ResteasyProviderFactory; import org.jboss.resteasy.spi.ResteasyProviderFactory;
import org.keycloak.Config; import org.keycloak.Config;
@ -15,6 +14,7 @@ import org.keycloak.models.utils.RepresentationToModel;
import org.keycloak.representations.idm.RealmRepresentation; import org.keycloak.representations.idm.RealmRepresentation;
import org.keycloak.representations.idm.UserRepresentation; import org.keycloak.representations.idm.UserRepresentation;
import org.keycloak.services.DefaultKeycloakSessionFactory; import org.keycloak.services.DefaultKeycloakSessionFactory;
import org.keycloak.services.ServicesLogger;
import org.keycloak.services.filters.KeycloakTransactionCommitter; import org.keycloak.services.filters.KeycloakTransactionCommitter;
import org.keycloak.services.managers.ApplianceBootstrap; import org.keycloak.services.managers.ApplianceBootstrap;
import org.keycloak.services.managers.RealmManager; import org.keycloak.services.managers.RealmManager;
@ -44,7 +44,7 @@ import java.util.*;
*/ */
public class KeycloakApplication extends Application { public class KeycloakApplication extends Application {
private static final Logger log = Logger.getLogger(KeycloakApplication.class); private static final ServicesLogger log = ServicesLogger.ROOT_LOGGER;
protected Set<Object> singletons = new HashSet<Object>(); protected Set<Object> singletons = new HashSet<Object>();
protected Set<Class<?>> classes = new HashSet<Class<?>>(); protected Set<Class<?>> classes = new HashSet<Class<?>>();
@ -135,7 +135,7 @@ public class KeycloakApplication extends Application {
session.getTransaction().commit(); session.getTransaction().commit();
} catch (Exception e) { } catch (Exception e) {
session.getTransaction().rollback(); session.getTransaction().rollback();
log.error("Failed to migrate datamodel", e); log.migrationFailure(e);
} finally { } finally {
session.close(); session.close();
} }
@ -163,7 +163,7 @@ public class KeycloakApplication extends Application {
if (configDir != null) { if (configDir != null) {
File f = new File(configDir + File.separator + "keycloak-server.json"); File f = new File(configDir + File.separator + "keycloak-server.json");
if (f.isFile()) { if (f.isFile()) {
log.info("Load config from " + f.getAbsolutePath()); log.loadingFrom(f.getAbsolutePath());
node = new ObjectMapper().readTree(f); node = new ObjectMapper().readTree(f);
} }
} }
@ -171,7 +171,7 @@ public class KeycloakApplication extends Application {
if (node == null) { if (node == null) {
URL resource = Thread.currentThread().getContextClassLoader().getResource("META-INF/keycloak-server.json"); URL resource = Thread.currentThread().getContextClassLoader().getResource("META-INF/keycloak-server.json");
if (resource != null) { if (resource != null) {
log.info("Load config from " + resource); log.loadingFrom(resource);
node = new ObjectMapper().readTree(resource); node = new ObjectMapper().readTree(resource);
} }
} }
@ -247,22 +247,22 @@ public class KeycloakApplication extends Application {
manager.setContextPath(getContextPath()); manager.setContextPath(getContextPath());
if (rep.getId() != null && manager.getRealm(rep.getId()) != null) { if (rep.getId() != null && manager.getRealm(rep.getId()) != null) {
log.info("Not importing realm " + rep.getRealm() + " from " + from + ". It already exists."); log.realmExists(rep.getRealm(), from);
return; return;
} }
if (manager.getRealmByName(rep.getRealm()) != null) { if (manager.getRealmByName(rep.getRealm()) != null) {
log.info("Not importing realm " + rep.getRealm() + " from " + from + ". It already exists."); log.realmExists(rep.getRealm(), from);
return; return;
} }
try { try {
RealmModel realm = manager.importRealm(rep); RealmModel realm = manager.importRealm(rep);
session.getTransaction().commit(); session.getTransaction().commit();
log.info("Imported realm " + realm.getName() + " from " + from); log.importedRealm(realm.getName(), from);
} catch (Throwable t) { } catch (Throwable t) {
session.getTransaction().rollback(); session.getTransaction().rollback();
log.warn("Unable to import realm " + rep.getRealm() + " from " + from + ". Cause: " + t.getMessage()); log.unableToImportRealm(t, rep.getRealm(), from);
} }
} finally { } finally {
session.close(); session.close();
@ -274,14 +274,14 @@ public class KeycloakApplication extends Application {
if (configDir != null) { if (configDir != null) {
File addUserFile = new File(configDir + File.separator + "keycloak-add-user.json"); File addUserFile = new File(configDir + File.separator + "keycloak-add-user.json");
if (addUserFile.isFile()) { if (addUserFile.isFile()) {
log.info("Importing users from '" + addUserFile + "'"); log.imprtingUsersFrom(addUserFile);
List<RealmRepresentation> realms; List<RealmRepresentation> realms;
try { try {
realms = JsonSerialization.readValue(new FileInputStream(addUserFile), new TypeReference<List<RealmRepresentation>>() { realms = JsonSerialization.readValue(new FileInputStream(addUserFile), new TypeReference<List<RealmRepresentation>>() {
}); });
} catch (IOException e) { } catch (IOException e) {
log.errorv("Failed to load 'keycloak-add-user.json': {0}", e.getMessage()); log.failedToLoadUsers(e);
return; return;
} }
@ -293,7 +293,7 @@ public class KeycloakApplication extends Application {
RealmModel realm = session.realms().getRealmByName(realmRep.getRealm()); RealmModel realm = session.realms().getRealmByName(realmRep.getRealm());
if (realm == null) { if (realm == null) {
log.errorv("Failed to add user ''{0}'' to realm ''{1}'': realm not found", userRep.getUsername(), realmRep.getRealm()); log.addUserFailedRealmNotFound(userRep.getUsername(), realmRep.getRealm());
} else { } else {
UserModel user = session.users().addUser(realm, userRep.getUsername()); UserModel user = session.users().addUser(realm, userRep.getUsername());
user.setEnabled(userRep.isEnabled()); user.setEnabled(userRep.isEnabled());
@ -302,12 +302,12 @@ public class KeycloakApplication extends Application {
} }
session.getTransaction().commit(); session.getTransaction().commit();
log.infov("Added user ''{0}'' to realm ''{1}''", userRep.getUsername(), realmRep.getRealm()); log.addUserSuccess(userRep.getUsername(), realmRep.getRealm());
} catch (ModelDuplicateException e) { } catch (ModelDuplicateException e) {
log.errorv("Failed to add user ''{0}'' to realm ''{1}'': user with username exists", userRep.getUsername(), realmRep.getRealm()); log.addUserFailedUserExists(userRep.getUsername(), realmRep.getRealm());
} catch (Throwable t) { } catch (Throwable t) {
session.getTransaction().rollback(); session.getTransaction().rollback();
log.errorv("Failed to add user ''{0}'' to realm ''{1}'': {2}", userRep.getUsername(), realmRep.getRealm(), t.getMessage()); log.addUserFailed(t, userRep.getUsername(), realmRep.getRealm());
} finally { } finally {
session.close(); session.close();
} }
@ -315,7 +315,7 @@ public class KeycloakApplication extends Application {
} }
if (!addUserFile.delete()) { if (!addUserFile.delete()) {
log.errorv("Failed to delete '{0}'", addUserFile.getAbsolutePath()); log.failedToDeleteFile(addUserFile.getAbsolutePath());
} }
} }
} }