Search in sources :

Example 11 with SessionLogEntry

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

the class EntityManagerTLRJUnitTestSuite method testMergeDetachedObject.

public void testMergeDetachedObject() {
    // Step 1 - read a department and clear the cache.
    clearCache("fieldaccess");
    EntityManager em = createEntityManager("fieldaccess");
    Query query = em.createNamedQuery("findAllSQLDepartments");
    Collection departments = query.getResultList();
    Department detachedDepartment;
    // and a second time with the department table empty.
    if (departments.isEmpty()) {
        beginTransaction(em);
        detachedDepartment = new Department();
        detachedDepartment.setName("Department X");
        em.persist(detachedDepartment);
        commitTransaction(em);
    } else {
        detachedDepartment = (Department) departments.iterator().next();
    }
    closeEntityManager(em);
    clearCache("fieldaccess");
    // Step 2 - create a new em, create a new employee with the
    // detached department and then query the departments again.
    em = createEntityManager("fieldaccess");
    beginTransaction(em);
    Employee emp = new Employee();
    emp.setFirstName("Crazy");
    emp.setLastName("Kid");
    emp.setId(41);
    emp.setDepartment(detachedDepartment);
    em.persist(emp);
    // the try/catch should be removed when the bug is fixed
    try {
        commitTransaction(em);
        em.createNamedQuery("findAllSQLDepartments").getResultList();
    } catch (RuntimeException e) {
        getServerSession("fieldaccess").log(new SessionLogEntry(getServerSession("fieldaccess"), SessionLog.WARNING, SessionLog.TRANSACTION, e));
    }
    closeEntityManager(em);
}
Also used : EntityManager(jakarta.persistence.EntityManager) JpaEntityManager(org.eclipse.persistence.jpa.JpaEntityManager) Query(jakarta.persistence.Query) ObjectLevelReadQuery(org.eclipse.persistence.queries.ObjectLevelReadQuery) DatabaseQuery(org.eclipse.persistence.queries.DatabaseQuery) JpaQuery(org.eclipse.persistence.jpa.JpaQuery) ReadAllQuery(org.eclipse.persistence.queries.ReadAllQuery) Collection(java.util.Collection) SessionLogEntry(org.eclipse.persistence.logging.SessionLogEntry)

Example 12 with SessionLogEntry

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

the class AbstractSession method logThrowable.

/**
 * PUBLIC:
 * <p>
 * Log a throwable with level and category.
 * </p>
 *
 * @param level  the log request level value
 * @param category  the string representation of a EclipseLink category.
 * @param throwable  a Throwable
 */
public void logThrowable(int level, String category, Throwable throwable) {
    if (this.isLoggingOff) {
        return;
    }
    // Must not create the log if not logging as is a performance issue.
    if (shouldLog(level, category)) {
        startOperationProfile(SessionProfiler.Logging);
        log(new SessionLogEntry(this, level, category, throwable));
        endOperationProfile(SessionProfiler.Logging);
    }
}
Also used : SessionLogEntry(org.eclipse.persistence.logging.SessionLogEntry)

Example 13 with SessionLogEntry

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

the class SLF4JLoggerHelper method createLogEntry.

/**
 * Create a new instance of {@link SessionLogEntry} class and set provided session, log level, logging category
 * and {@link String} message to it.
 * @param level    Log level of the new log entry.
 * @param category Logging category of the new log entry.
 * @param message  {@link String} message of the new log entry.
 * @return The new instance of {@link SessionLogEntry} class with all provided values set.
 */
private SessionLogEntry createLogEntry(final LogCategory category, final LogLevel level, final String message) {
    final SessionLogEntry logEntry = initLogEntry(category, level);
    logEntry.setMessage(message);
    return logEntry;
}
Also used : SessionLogEntry(org.eclipse.persistence.logging.SessionLogEntry)

Example 14 with SessionLogEntry

use of org.eclipse.persistence.logging.SessionLogEntry 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 15 with SessionLogEntry

use of org.eclipse.persistence.logging.SessionLogEntry 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)

Aggregations

SessionLogEntry (org.eclipse.persistence.logging.SessionLogEntry)18 Logger (ch.qos.logback.classic.Logger)3 ILoggingEvent (ch.qos.logback.classic.spi.ILoggingEvent)3 LogCategory (org.eclipse.persistence.logging.LogCategory)3 LogLevel (org.eclipse.persistence.logging.LogLevel)3 SLF4JLogger (org.eclipse.persistence.logging.slf4j.SLF4JLogger)3 EntityManager (jakarta.persistence.EntityManager)2 Query (jakarta.persistence.Query)2 Collection (java.util.Collection)2 ClassReader (org.eclipse.persistence.internal.libraries.asm.ClassReader)2 EclipseLinkClassReader (org.eclipse.persistence.internal.libraries.asm.EclipseLinkClassReader)2 JpaEntityManager (org.eclipse.persistence.jpa.JpaEntityManager)2 JpaQuery (org.eclipse.persistence.jpa.JpaQuery)2 AbstractSessionLog (org.eclipse.persistence.logging.AbstractSessionLog)2 SessionLog (org.eclipse.persistence.logging.SessionLog)2 DatabaseQuery (org.eclipse.persistence.queries.DatabaseQuery)2 ObjectLevelReadQuery (org.eclipse.persistence.queries.ObjectLevelReadQuery)2 ReadAllQuery (org.eclipse.persistence.queries.ReadAllQuery)2 IOException (java.io.IOException)1 InputStream (java.io.InputStream)1