Search in sources :

Example 6 with LogLevel

use of org.eclipse.persistence.logging.LogLevel in project eclipselink by eclipse-ee4j.

the class SLF4JLoggerHelper method testLogMessage.

/**
 * Test {@code SLF4JLogger.log(SessionLogEntry)} method with regular {@link String} message.
 * Matrix of logger level x category settings with all log entry log levels is being checked.
 */
public void testLogMessage() {
    // Verify loggers for logger level x category matrix.
    for (LogCategory category : LogCategory.values()) {
        final String nameSpace = category.getNameSpace();
        final Logger categoryLogger = loggerContext.getLogger(nameSpace);
        categoryLogger.setLevel(Level.ALL);
        for (LogLevel loggerLevel : LogLevel.values()) {
            // Verify messages with all log levels.
            logger.setLevel(loggerLevel.getId(), category.getName());
            for (LogLevel messageLevel : LogLevel.values()) {
                final String message = "Log message";
                final SessionLogEntry logEntry = createLogEntry(category, messageLevel, message);
                // Logback log event additional check.
                final Check check = new Check() {

                    @Override
                    public void check(final ILoggingEvent logEvent) {
                        assertEquals("Logged message \"" + message + "\" must be stored as a message.", message, logEvent.getMessage());
                        assertNull("There can't be any arguments for already rendered message.", logEvent.getArgumentArray());
                    }
                };
                testLogEntry(category, loggerLevel, messageLevel, categoryLogger, logEntry, check);
            }
        }
    }
}
Also used : LogCategory(org.eclipse.persistence.logging.LogCategory) Logger(ch.qos.logback.classic.Logger) SLF4JLogger(org.eclipse.persistence.logging.slf4j.SLF4JLogger) ILoggingEvent(ch.qos.logback.classic.spi.ILoggingEvent) LogLevel(org.eclipse.persistence.logging.LogLevel) SessionLogEntry(org.eclipse.persistence.logging.SessionLogEntry)

Example 7 with LogLevel

use of org.eclipse.persistence.logging.LogLevel in project eclipselink by eclipse-ee4j.

the class LogLevelHelper method testToValueInt.

/**
 * Test {@code LogLevel.toValue(int)} method.
 */
public static void testToValueInt() {
    // Check valid IDs.
    for (LogLevel level : LogLevel.values()) {
        int id = level.getId();
        LogLevel levelValue = LogLevel.toValue(id);
        assertEquals("Log level was not found for ID: " + id, level, levelValue);
    }
    // Check some invalid IDs.
    final int[] invalidIds = new int[] { -2, -1, LogLevel.length, LogLevel.length + 1 };
    for (int id : invalidIds) {
        try {
            LogLevel.toValue(-1);
            fail("LogLevel.toValue(" + id + ") shall throw IllegalArgumentException");
        } catch (IllegalArgumentException ex) {
        // This exception is expected for illegal IDs.
        }
    }
}
Also used : LogLevel(org.eclipse.persistence.logging.LogLevel)

Example 8 with LogLevel

use of org.eclipse.persistence.logging.LogLevel in project eclipselink by eclipse-ee4j.

the class SLF4JLoggerHelper method testLogExceptionStackTrace.

/**
 * Test {@code SLF4JLogger.log(SessionLogEntry)} method with {@link Throwable} and stack trace logging turned on.
 * Matrix of logger level x category settings with all log entry log levels is being checked.
 */
public void testLogExceptionStackTrace() {
    // Verify loggers for logger level x category matrix.
    for (LogCategory category : LogCategory.values()) {
        final String nameSpace = category.getNameSpace();
        final Logger categoryLogger = loggerContext.getLogger(nameSpace);
        categoryLogger.setLevel(Level.ALL);
        for (LogLevel loggerLevel : LogLevel.values()) {
            // Verify messages with all log levels.
            logger.setLevel(loggerLevel.getId(), category.getName());
            for (LogLevel messageLevel : LogLevel.values()) {
                final String message = "Log message";
                final String exceptionMessage = "Exception message";
                final Throwable exception = new RuntimeException(exceptionMessage);
                // Log entry without log message.
                final SessionLogEntry logEntry1 = createLogEntry(category, messageLevel, exception);
                // Log entry with log message.
                final SessionLogEntry logEntry2 = createLogEntry(category, messageLevel, message, exception);
                // Logback log event additional check for exception without log message.
                final Check check1 = new Check() {

                    @Override
                    public void check(final ILoggingEvent logEvent) {
                        final String eventMessage = logEvent.getMessage();
                        assertTrue("No message was passed so null or empty String must be stored as a message.", eventMessage == null || eventMessage.isEmpty());
                        assertEquals("Exception message must be stored in throwableProxy.", exceptionMessage, logEvent.getThrowableProxy().getMessage());
                    }
                };
                // Logback log event additional check for exception with log message.
                final Check check2 = new Check() {

                    @Override
                    public void check(final ILoggingEvent logEvent) {
                        assertEquals("Logged message \"" + message + "\" must be stored as a message.", message, logEvent.getMessage());
                        assertEquals("Exception message must be stored in throwableProxy.", exceptionMessage, logEvent.getThrowableProxy().getMessage());
                    }
                };
                testLogEntry(category, loggerLevel, messageLevel, categoryLogger, logEntry1, check1);
                testLogEntry(category, loggerLevel, messageLevel, categoryLogger, logEntry2, check2);
            }
        }
    }
}
Also used : LogCategory(org.eclipse.persistence.logging.LogCategory) Logger(ch.qos.logback.classic.Logger) SLF4JLogger(org.eclipse.persistence.logging.slf4j.SLF4JLogger) ILoggingEvent(ch.qos.logback.classic.spi.ILoggingEvent) LogLevel(org.eclipse.persistence.logging.LogLevel) SessionLogEntry(org.eclipse.persistence.logging.SessionLogEntry)

Example 9 with LogLevel

use of org.eclipse.persistence.logging.LogLevel in project eclipselink by eclipse-ee4j.

the class SLF4JLoggerHelper method testLogExceptionWithoutStackTrace.

/**
 * Test {@code SLF4JLogger.log(SessionLogEntry)} method with {@link Throwable} and stack trace logging turned off.
 * Matrix of logger level x category settings with all log entry log levels is being checked.
 */
public void testLogExceptionWithoutStackTrace() {
    // Verify loggers for logger level x category matrix.
    for (LogCategory category : LogCategory.values()) {
        final String nameSpace = category.getNameSpace();
        final Logger categoryLogger = loggerContext.getLogger(nameSpace);
        categoryLogger.setLevel(Level.ALL);
        for (LogLevel loggerLevel : LogLevel.values()) {
            // Verify messages with all log levels.
            logger.setLevel(loggerLevel.getId(), category.getName());
            for (LogLevel messageLevel : LogLevel.values()) {
                final Throwable exception = new RuntimeException("Exception message");
                final SessionLogEntry logEntry = createLogEntry(category, messageLevel, exception);
                // Logback log event additional check.
                final Check check = new Check() {

                    @Override
                    public void check(final ILoggingEvent logEvent) {
                        assertEquals("Logged exception message \"" + exception.getMessage() + "\" must be stored as a message.", exception.toString(), logEvent.getMessage());
                        assertNull("There can't be any arguments for already rendered message.", logEvent.getArgumentArray());
                    }
                };
                testLogEntry(category, loggerLevel, messageLevel, categoryLogger, logEntry, check);
            }
        }
    }
}
Also used : LogCategory(org.eclipse.persistence.logging.LogCategory) Logger(ch.qos.logback.classic.Logger) SLF4JLogger(org.eclipse.persistence.logging.slf4j.SLF4JLogger) ILoggingEvent(ch.qos.logback.classic.spi.ILoggingEvent) LogLevel(org.eclipse.persistence.logging.LogLevel) SessionLogEntry(org.eclipse.persistence.logging.SessionLogEntry)

Example 10 with LogLevel

use of org.eclipse.persistence.logging.LogLevel in project eclipselink by eclipse-ee4j.

the class JAXBMarshaller method setProperty.

/**
 * Set a property on the JAXBMarshaller. Attempting to set any unsupported
 * property will result in a jakarta.xml.bind.PropertyException
 * @see org.eclipse.persistence.jaxb.MarshallerProperties
 */
@Override
public void setProperty(String key, Object value) throws PropertyException {
    try {
        if (key == null) {
            throw new IllegalArgumentException();
        } else {
            SessionLog logger = AbstractSessionLog.getLog();
            if (logger.shouldLog(SessionLog.FINE, SessionLog.MOXY)) {
                logger.log(SessionLog.FINE, SessionLog.MOXY, "moxy_set_marshaller_property", new Object[] { key, value });
            }
            if (MOXySystemProperties.moxyLogPayload != null && xmlMarshaller.isLogPayload() == null) {
                xmlMarshaller.setLogPayload(MOXySystemProperties.moxyLogPayload);
            }
            if (Constants.JAXB_FRAGMENT.equals(key)) {
                if (value == null) {
                    throw new PropertyException(key, Constants.EMPTY_STRING);
                }
                Boolean fragment = (Boolean) value;
                xmlMarshaller.setFragment(fragment);
            } else if (JAXB_FORMATTED_OUTPUT.equals(key)) {
                if (value == null) {
                    throw new PropertyException(key, Constants.EMPTY_STRING);
                }
                Boolean formattedOutput = (Boolean) value;
                xmlMarshaller.setFormattedOutput(formattedOutput);
            } else if (JAXB_ENCODING.equals(key)) {
                xmlMarshaller.setEncoding((String) value);
            } else if (JAXB_SCHEMA_LOCATION.equals(key)) {
                xmlMarshaller.setSchemaLocation((String) value);
            } else if (JAXB_NO_NAMESPACE_SCHEMA_LOCATION.equals(key)) {
                xmlMarshaller.setNoNamespaceSchemaLocation((String) value);
            } else if (MarshallerProperties.NAMESPACE_PREFIX_MAPPER.equals(key)) {
                if (value == null) {
                    xmlMarshaller.setNamespacePrefixMapper(null);
                } else if (value instanceof Map) {
                    NamespacePrefixMapper namespacePrefixMapper = new MapNamespacePrefixMapper((Map) value);
                    xmlMarshaller.setNamespacePrefixMapper(namespacePrefixMapper);
                } else {
                    xmlMarshaller.setNamespacePrefixMapper((NamespacePrefixMapper) value);
                }
            } else if (SUN_NAMESPACE_PREFIX_MAPPER.equals(key) || SUN_JSE_NAMESPACE_PREFIX_MAPPER.equals(key)) {
                if (value == null) {
                    xmlMarshaller.setNamespacePrefixMapper(null);
                } else {
                    xmlMarshaller.setNamespacePrefixMapper(new NamespacePrefixMapperWrapper(value));
                }
            } else if (MarshallerProperties.INDENT_STRING.equals(key) || SUN_INDENT_STRING.equals(key) || SUN_JSE_INDENT_STRING.equals(key)) {
                xmlMarshaller.setIndentString((String) value);
            } else if (MarshallerProperties.JSON_MARSHAL_EMPTY_COLLECTIONS.equals(key)) {
                xmlMarshaller.setMarshalEmptyCollections((Boolean) value);
            } else if (MarshallerProperties.JSON_REDUCE_ANY_ARRAYS.equals(key)) {
                xmlMarshaller.setReduceAnyArrays((Boolean) value);
            } else if (MarshallerProperties.JSON_WRAPPER_AS_ARRAY_NAME.equals(key)) {
                xmlMarshaller.setWrapperAsCollectionName((Boolean) value);
            } else if (MarshallerProperties.JSON_USE_XSD_TYPES_WITH_PREFIX.equals(key)) {
                xmlMarshaller.getJsonTypeConfiguration().setUseXsdTypesWithPrefix((Boolean) value);
            } else if (MarshallerProperties.JSON_TYPE_COMPATIBILITY.equals(key)) {
                xmlMarshaller.getJsonTypeConfiguration().setJsonTypeCompatibility((Boolean) value);
            } else if (MarshallerProperties.JSON_TYPE_ATTRIBUTE_NAME.equals(key)) {
                xmlMarshaller.getJsonTypeConfiguration().setJsonTypeAttributeName((String) value);
            } else if (MarshallerProperties.JSON_DISABLE_NESTED_ARRAY_NAME.equals(key)) {
                xmlMarshaller.getJsonTypeConfiguration().setJsonDisableNestedArrayName((Boolean) value);
            } else if (MarshallerProperties.CHARACTER_ESCAPE_HANDLER.equals(key)) {
                xmlMarshaller.setCharacterEscapeHandler((CharacterEscapeHandler) value);
            } else if (MarshallerProperties.MOXY_LOG_PAYLOAD.equals(key)) {
                xmlMarshaller.setLogPayload(((Boolean) value));
            } else if (MarshallerProperties.MOXY_LOGGING_LEVEL.equals(key)) {
                if (value instanceof String) {
                    AbstractSessionLog.getLog().setLevel(LogLevel.toValue((String) value).getId(), SessionLog.MOXY);
                } else {
                    AbstractSessionLog.getLog().setLevel(((LogLevel) value).getId(), SessionLog.MOXY);
                }
            } else if (SUN_CHARACTER_ESCAPE_HANDLER.equals(key) || SUN_JSE_CHARACTER_ESCAPE_HANDLER.equals(key) || SUN_CHARACTER_ESCAPE_HANDLER_MARSHALLER.equals(key) || SUN_JSE_CHARACTER_ESCAPE_HANDLER_MARSHALLER.equals(key)) {
                if (value == null) {
                    xmlMarshaller.setCharacterEscapeHandler(null);
                } else {
                    xmlMarshaller.setCharacterEscapeHandler(new CharacterEscapeHandlerWrapper(value));
                }
            } else if (XML_DECLARATION.equals(key)) {
                if (value == null) {
                    throw new PropertyException(key, Constants.EMPTY_STRING);
                }
                Boolean fragment = !(Boolean) value;
                xmlMarshaller.setFragment(fragment);
            } else if (XML_HEADERS.equals(key)) {
                xmlMarshaller.setXmlHeader((String) value);
            } else if (OBJECT_IDENTITY_CYCLE_DETECTION.equals(key)) {
                if (value == null) {
                    throw new PropertyException(key, Constants.EMPTY_STRING);
                }
                xmlMarshaller.setEqualUsingIdenity((Boolean) value);
            } else if (MarshallerProperties.MEDIA_TYPE.equals(key)) {
                MediaType mType = null;
                if (value instanceof MediaType) {
                    mType = (MediaType) value;
                } else if (value instanceof String) {
                    mType = MediaType.getMediaType((String) value);
                }
                if (mType == null) {
                    throw new PropertyException(key, Constants.EMPTY_STRING);
                }
                xmlMarshaller.setMediaType(mType);
            } else if (MarshallerProperties.JSON_ATTRIBUTE_PREFIX.equals(key)) {
                xmlMarshaller.setAttributePrefix((String) value);
            } else if (MarshallerProperties.JSON_INCLUDE_ROOT.equals(key)) {
                if (value == null) {
                    throw new PropertyException(key, Constants.EMPTY_STRING);
                }
                xmlMarshaller.setIncludeRoot((Boolean) value);
            } else if (MarshallerProperties.JSON_VALUE_WRAPPER.equals(key)) {
                if (value == null || (((String) value).length() == 0)) {
                    throw new PropertyException(key, Constants.EMPTY_STRING);
                }
                xmlMarshaller.setValueWrapper((String) value);
            } else if (MarshallerProperties.JSON_NAMESPACE_SEPARATOR.equals(key)) {
                if (value == null) {
                    throw new PropertyException(key, Constants.EMPTY_STRING);
                }
                xmlMarshaller.setNamespaceSeparator((Character) value);
            } else if (MarshallerProperties.OBJECT_GRAPH.equals(key)) {
                if (value == null) {
                    xmlMarshaller.setMarshalAttributeGroup(null);
                } else if (value instanceof ObjectGraphImpl) {
                    xmlMarshaller.setMarshalAttributeGroup(((ObjectGraphImpl) value).getAttributeGroup());
                } else if (value.getClass() == ClassConstants.STRING) {
                    xmlMarshaller.setMarshalAttributeGroup(value);
                } else {
                    throw org.eclipse.persistence.exceptions.JAXBException.invalidValueForObjectGraph(value);
                }
            } else if (MarshallerProperties.BEAN_VALIDATION_MODE.equals(key)) {
                if (value == null) {
                    throw new PropertyException(key, Constants.EMPTY_STRING);
                }
                this.beanValidationMode = ((BeanValidationMode) value);
            } else if (MarshallerProperties.BEAN_VALIDATION_FACTORY.equals(key)) {
                // noinspection StatementWithEmptyBody
                if (value == null) {
                // Allow null value for preferred validation factory.
                }
                this.prefValidatorFactory = value;
            } else if (MarshallerProperties.BEAN_VALIDATION_GROUPS.equals(key)) {
                if (value == null) {
                    throw new PropertyException(key, Constants.EMPTY_STRING);
                }
                this.beanValidationGroups = ((Class<?>[]) value);
            } else if (MarshallerProperties.BEAN_VALIDATION_NO_OPTIMISATION.equals(key)) {
                if (value == null) {
                    throw new PropertyException(key, Constants.EMPTY_STRING);
                }
                this.bvNoOptimisation = ((boolean) value);
            } else {
                throw new PropertyException(key, value);
            }
        }
    } catch (ClassCastException exception) {
        throw new PropertyException(key, exception);
    }
}
Also used : MapNamespacePrefixMapper(org.eclipse.persistence.internal.oxm.record.namespaces.MapNamespacePrefixMapper) PropertyException(jakarta.xml.bind.PropertyException) CharacterEscapeHandler(org.eclipse.persistence.oxm.CharacterEscapeHandler) ObjectGraphImpl(org.eclipse.persistence.internal.jaxb.ObjectGraphImpl) LogLevel(org.eclipse.persistence.logging.LogLevel) SessionLog(org.eclipse.persistence.logging.SessionLog) AbstractSessionLog(org.eclipse.persistence.logging.AbstractSessionLog) MediaType(org.eclipse.persistence.oxm.MediaType) NamespacePrefixMapperWrapper(org.eclipse.persistence.internal.oxm.record.namespaces.NamespacePrefixMapperWrapper) Map(java.util.Map) HashMap(java.util.HashMap) NamespacePrefixMapper(org.eclipse.persistence.oxm.NamespacePrefixMapper) MapNamespacePrefixMapper(org.eclipse.persistence.internal.oxm.record.namespaces.MapNamespacePrefixMapper)

Aggregations

LogLevel (org.eclipse.persistence.logging.LogLevel)10 LogCategory (org.eclipse.persistence.logging.LogCategory)5 Logger (ch.qos.logback.classic.Logger)3 ILoggingEvent (ch.qos.logback.classic.spi.ILoggingEvent)3 SessionLogEntry (org.eclipse.persistence.logging.SessionLogEntry)3 SLF4JLogger (org.eclipse.persistence.logging.slf4j.SLF4JLogger)3 PropertyException (jakarta.xml.bind.PropertyException)2 HashMap (java.util.HashMap)2 Map (java.util.Map)2 ObjectGraphImpl (org.eclipse.persistence.internal.jaxb.ObjectGraphImpl)2 AbstractSessionLog (org.eclipse.persistence.logging.AbstractSessionLog)2 SessionLog (org.eclipse.persistence.logging.SessionLog)2 MediaType (org.eclipse.persistence.oxm.MediaType)2 NamespacePrefixMapper (org.eclipse.persistence.oxm.NamespacePrefixMapper)2 Logger (org.slf4j.Logger)2 Entry (java.util.Map.Entry)1 IDResolverWrapper (org.eclipse.persistence.internal.jaxb.IDResolverWrapper)1 MapNamespacePrefixMapper (org.eclipse.persistence.internal.oxm.record.namespaces.MapNamespacePrefixMapper)1 NamespacePrefixMapperWrapper (org.eclipse.persistence.internal.oxm.record.namespaces.NamespacePrefixMapperWrapper)1 PrefixMapperNamespaceResolver (org.eclipse.persistence.internal.oxm.record.namespaces.PrefixMapperNamespaceResolver)1