use of java.util.MissingResourceException in project Payara by payara.
the class JSONLogFormatter method jsonLogFormat.
/**
* @param record The record to format.
* @return The JSON formatted record.
*/
private String jsonLogFormat(LogRecord record) {
try {
LogEventImpl logEvent = new LogEventImpl();
JsonObjectBuilder eventObject = Json.createObjectBuilder();
/*
* Create the timestamp field and append to object.
*/
SimpleDateFormat dateFormatter;
if (null != getRecordDateFormat()) {
dateFormatter = new SimpleDateFormat(getRecordDateFormat());
} else {
dateFormatter = new SimpleDateFormat(RFC3339_DATE_FORMAT);
}
date.setTime(record.getMillis());
String timestampValue = dateFormatter.format(date);
logEvent.setTimestamp(timestampValue);
eventObject.add(TIMESTAMP_KEY, timestampValue);
/*
* Create the event level field and append to object.
*/
Level eventLevel = record.getLevel();
logEvent.setLevel(eventLevel.getName());
StringBuilder levelBuilder = new StringBuilder();
levelBuilder.append(eventLevel.getLocalizedName());
eventObject.add(LOG_LEVEL_KEY, levelBuilder.toString());
/*
* Get the product id and append to object.
*/
productId = getProductId();
logEvent.setComponentId(productId);
eventObject.add(PRODUCT_ID_KEY, productId);
/*
* Get the logger name and append to object.
*/
String loggerName = record.getLoggerName();
if (null == loggerName) {
loggerName = "";
}
logEvent.setLogger(loggerName);
StringBuilder loggerBuilder = new StringBuilder();
loggerBuilder.append(loggerName);
eventObject.add(LOGGER_NAME_KEY, loggerBuilder.toString());
/*
* Get thread information and append to object if not excluded.
*/
if (!excludeFieldsSupport.isSet(ExcludeFieldsSupport.SupplementalAttribute.TID)) {
// Thread ID
int threadId = record.getThreadID();
logEvent.setThreadId(threadId);
eventObject.add(THREAD_ID_KEY, String.valueOf(threadId));
// Thread Name
String threadName;
if (record instanceof GFLogRecord) {
threadName = ((GFLogRecord) record).getThreadName();
} else {
threadName = Thread.currentThread().getName();
}
logEvent.setThreadName(threadName);
eventObject.add(THREAD_NAME_KEY, threadName);
}
/*
* Get user id and append if not excluded and exists with value.
*/
if (!excludeFieldsSupport.isSet(ExcludeFieldsSupport.SupplementalAttribute.USERID)) {
String userId = logEvent.getUser();
if (null != userId && !userId.isEmpty()) {
eventObject.add(USER_ID_KEY, userId);
}
}
/*
* Get ec id and append if not excluded and exists with value.
*/
if (!excludeFieldsSupport.isSet(ExcludeFieldsSupport.SupplementalAttribute.ECID)) {
String ecid = logEvent.getECId();
if (null != ecid && !ecid.isEmpty()) {
eventObject.add(ECID_KEY, ecid);
}
}
/*
* Get millis time for log entry timestamp
*/
if (!excludeFieldsSupport.isSet(ExcludeFieldsSupport.SupplementalAttribute.TIME_MILLIS)) {
Long timestamp = record.getMillis();
logEvent.setTimeMillis(timestamp);
eventObject.add(TIME_MILLIS_KEY, String.valueOf(timestamp));
}
/*
* Include the integer value for log level
*/
Level level = record.getLevel();
if (!excludeFieldsSupport.isSet(ExcludeFieldsSupport.SupplementalAttribute.LEVEL_VALUE)) {
int levelValue = level.intValue();
logEvent.setLevelValue(levelValue);
eventObject.add(LEVEL_VALUE_KEY, String.valueOf(levelValue));
}
/*
* Stick the message id on the entry
*/
String messageId = getMessageId(record);
if (messageId != null && !messageId.isEmpty()) {
logEvent.setMessageId(messageId);
eventObject.add(MESSAGE_ID_KEY, messageId);
}
/*
* Include ClassName and MethodName for FINER and FINEST log levels.
*/
if (LOG_SOURCE_IN_KEY_VALUE || level.intValue() <= Level.FINE.intValue()) {
String sourceClassName = record.getSourceClassName();
if (null != sourceClassName && !sourceClassName.isEmpty()) {
logEvent.getSupplementalAttributes().put(CLASS_NAME, sourceClassName);
eventObject.add(CLASS_NAME, sourceClassName);
}
String sourceMethodName = record.getSourceMethodName();
if (null != sourceMethodName && !sourceMethodName.isEmpty()) {
logEvent.getSupplementalAttributes().put(METHOD_NAME, sourceMethodName);
eventObject.add(METHOD_NAME, sourceMethodName);
}
}
/*
* Add the record number to the entry.
*/
if (RECORD_NUMBER_IN_KEY_VALUE) {
recordNumber++;
logEvent.getSupplementalAttributes().put(RECORD_NUMBER, recordNumber);
eventObject.add(RECORD_NUMBER, String.valueOf(recordNumber));
}
if (null != _delegate) {
_delegate.format(new StringBuilder().append(eventObject.toString()), level);
}
String logMessage = record.getMessage();
if (null == logMessage || logMessage.trim().equals("")) {
Throwable throwable = record.getThrown();
if (null != throwable) {
try (StringWriter stringWriter = new StringWriter();
PrintWriter printWriter = new PrintWriter(stringWriter)) {
JsonObjectBuilder traceObject = Json.createObjectBuilder();
throwable.printStackTrace(printWriter);
logMessage = stringWriter.toString();
traceObject.add(EXCEPTION_KEY, throwable.getMessage());
traceObject.add(STACK_TRACE_KEY, logMessage);
logEvent.setMessage(logMessage);
eventObject.add(LOG_MESSAGE_KEY, traceObject.build());
}
}
} else {
if (logMessage.contains("{0") && logMessage.contains("}") && null != record.getParameters()) {
logMessage = MessageFormat.format(logMessage, record.getParameters());
} else {
ResourceBundle bundle = getResourceBundle(record.getLoggerName());
if (null != bundle) {
try {
logMessage = MessageFormat.format(bundle.getString(logMessage), record.getParameters());
} catch (MissingResourceException ex) {
// Leave logMessage as it is because it already has
// an exception message
}
}
}
StringBuilder logMessageBuilder = new StringBuilder();
logMessageBuilder.append(logMessage);
Throwable throwable = getThrowable(record);
if (null != throwable) {
try (StringWriter stringWriter = new StringWriter();
PrintWriter printWriter = new PrintWriter(stringWriter)) {
JsonObjectBuilder traceObject = Json.createObjectBuilder();
throwable.printStackTrace(printWriter);
logMessage = stringWriter.toString();
traceObject.add(EXCEPTION_KEY, logMessageBuilder.toString());
traceObject.add(STACK_TRACE_KEY, logMessage);
logEvent.setMessage(logMessage);
eventObject.add(LOG_MESSAGE_KEY, traceObject.build());
}
} else {
logMessage = logMessageBuilder.toString();
logEvent.setMessage(logMessage);
eventObject.add(LOG_MESSAGE_KEY, logMessage);
}
}
informLogEventListeners(logEvent);
return eventObject.build().toString() + LINE_SEPARATOR;
} catch (Exception ex) {
new ErrorManager().error("Error in formatting Logrecord", ex, ErrorManager.FORMAT_FAILURE);
return "";
}
}
use of java.util.MissingResourceException in project OpenAM by OpenRock.
the class AMModelBase method getLocalizedServiceName.
/**
* Returns the localized service name.
*
* @param service Name of service.
* @param defaultValue Default value of service name if localized service
* name cannot be determine.
* @return the localized service name.
*/
public String getLocalizedServiceName(String service, String defaultValue) {
String i18nName = defaultValue;
try {
ServiceSchemaManager mgr = new ServiceSchemaManager(service, ssoToken);
String rbName = mgr.getI18NFileName();
if ((rbName != null) && (rbName.trim().length() > 0)) {
ResourceBundle rb = AMResBundleCacher.getBundle(rbName, locale);
String i18nKey = null;
Set types = mgr.getSchemaTypes();
if (!types.isEmpty()) {
SchemaType type = (SchemaType) types.iterator().next();
ServiceSchema schema = mgr.getSchema(type);
if (schema != null) {
i18nKey = schema.getI18NKey();
}
}
if ((i18nKey != null) && (i18nKey.length() > 0)) {
i18nName = Locale.getString(rb, i18nKey, debug);
}
}
} catch (SSOException e) {
debug.warning("AMModelBase.getLocalizedServiceName", e);
} catch (SMSException e) {
debug.warning("AMModelBase.getLocalizedServiceName", e);
} catch (MissingResourceException e) {
debug.warning("AMModelBase.getLocalizedServiceName", e);
}
return i18nName;
}
use of java.util.MissingResourceException in project tomee by apache.
the class AbstractDelegatingLogger method formatMessage.
protected String formatMessage(final LogRecord record) {
String format = record.getMessage();
final ResourceBundle catalog = record.getResourceBundle();
if (catalog != null) {
try {
format = catalog.getString(record.getMessage());
} catch (final MissingResourceException ex) {
format = record.getMessage();
}
}
try {
final Object[] parameters = record.getParameters();
if (parameters == null || parameters.length == 0) {
return format;
}
if (format.indexOf("{0") >= 0 || format.indexOf("{1") >= 0 || format.indexOf("{2") >= 0 || format.indexOf("{3") >= 0) {
return MessageFormat.format(format, parameters);
}
return format;
} catch (final Exception ex) {
return format;
}
}
use of java.util.MissingResourceException in project tomee by apache.
the class Messages method format.
public String format(final String message, final Object... args) {
init();
if (locale != _globalLocale) {
synchronized (Messages.class) {
init();
}
}
MessageFormat mf;
final String msg;
try {
mf = (MessageFormat) formats.get(message);
if (mf == null) {
try {
msg = messages.getString(message);
} catch (final MissingResourceException except) {
return message + (args != null ? " " + Arrays.toString(args) : "");
}
mf = new MessageFormat(msg);
formats.put(message, mf);
}
return mf.format(args);
} catch (final Exception except) {
return "An internal error occured while processing message " + message;
}
}
use of java.util.MissingResourceException in project killbill by killbill.
the class DefaultResourceBundleFactory method getGlobalBundle.
private ResourceBundle getGlobalBundle(final Locale locale, final String bundlePath) {
try {
// Try to loadDefaultCatalog the bundle from the classpath first
return ResourceBundle.getBundle(bundlePath, locale);
} catch (MissingResourceException ignored) {
}
// Try to loadDefaultCatalog it from a properties file
final String propertiesFileNameWithCountry = bundlePath + "_" + locale.getLanguage() + "_" + locale.getCountry() + ".properties";
ResourceBundle bundle = getBundleFromPropertiesFile(propertiesFileNameWithCountry);
if (bundle != null) {
return bundle;
} else {
final String propertiesFileName = bundlePath + "_" + locale.getLanguage() + ".properties";
bundle = getBundleFromPropertiesFile(propertiesFileName);
}
return bundle;
}
Aggregations