Search in sources :

Example 81 with MissingResourceException

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 "";
    }
}
Also used : ErrorManager(java.util.logging.ErrorManager) MissingResourceException(java.util.MissingResourceException) LogEventImpl(com.sun.enterprise.server.logging.LogEventImpl) MissingResourceException(java.util.MissingResourceException) StringWriter(java.io.StringWriter) GFLogRecord(com.sun.common.util.logging.GFLogRecord) Level(java.util.logging.Level) ResourceBundle(java.util.ResourceBundle) JsonObjectBuilder(javax.json.JsonObjectBuilder) SimpleDateFormat(java.text.SimpleDateFormat) PrintWriter(java.io.PrintWriter)

Example 82 with MissingResourceException

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;
}
Also used : ServiceSchema(com.sun.identity.sm.ServiceSchema) TreeSet(java.util.TreeSet) HashSet(java.util.HashSet) Set(java.util.Set) SMSException(com.sun.identity.sm.SMSException) MissingResourceException(java.util.MissingResourceException) ResourceBundle(java.util.ResourceBundle) SSOException(com.iplanet.sso.SSOException) ServiceSchemaManager(com.sun.identity.sm.ServiceSchemaManager) SchemaType(com.sun.identity.sm.SchemaType)

Example 83 with MissingResourceException

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;
    }
}
Also used : MissingResourceException(java.util.MissingResourceException) ResourceBundle(java.util.ResourceBundle) MissingResourceException(java.util.MissingResourceException)

Example 84 with MissingResourceException

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;
    }
}
Also used : MessageFormat(java.text.MessageFormat) MissingResourceException(java.util.MissingResourceException) MissingResourceException(java.util.MissingResourceException)

Example 85 with MissingResourceException

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;
}
Also used : MissingResourceException(java.util.MissingResourceException) PropertyResourceBundle(java.util.PropertyResourceBundle) ResourceBundle(java.util.ResourceBundle)

Aggregations

MissingResourceException (java.util.MissingResourceException)163 ResourceBundle (java.util.ResourceBundle)85 Locale (java.util.Locale)67 ArrayList (java.util.ArrayList)13 IOException (java.io.IOException)10 MessageFormat (java.text.MessageFormat)8 HashMap (java.util.HashMap)8 Map (java.util.Map)8 File (java.io.File)7 PropertyResourceBundle (java.util.PropertyResourceBundle)7 SMSException (com.sun.identity.sm.SMSException)6 Secure.getString (android.provider.Settings.Secure.getString)5 SSOException (com.iplanet.sso.SSOException)5 Iterator (java.util.Iterator)5 Set (java.util.Set)5 InputStream (java.io.InputStream)4 HashSet (java.util.HashSet)4 ISResourceBundle (com.sun.identity.common.ISResourceBundle)3 SimpleDateFormat (java.text.SimpleDateFormat)3 Enumeration (java.util.Enumeration)3