Search in sources :

Example 1 with LogEventImpl

use of com.sun.enterprise.server.logging.LogEventImpl 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)

Aggregations

GFLogRecord (com.sun.common.util.logging.GFLogRecord)1 LogEventImpl (com.sun.enterprise.server.logging.LogEventImpl)1 PrintWriter (java.io.PrintWriter)1 StringWriter (java.io.StringWriter)1 SimpleDateFormat (java.text.SimpleDateFormat)1 MissingResourceException (java.util.MissingResourceException)1 ResourceBundle (java.util.ResourceBundle)1 ErrorManager (java.util.logging.ErrorManager)1 Level (java.util.logging.Level)1 JsonObjectBuilder (javax.json.JsonObjectBuilder)1