LaVOZs

The World’s Largest Online Community for Developers

'; How to get values from logs into java list? <hibernate> - LavOzs.Com

Is this possible to take that values from console: enter image description here and save them into java list/map?

I printed that logs in that way:

logging.level.org.hibernate.SQL=DEBUG
logging.level.org.hibernate.type=TRACE
logging.level.org.hibernate.type.descriptor.sql.BasicBinder=TRACE

My query:

@Transactional
public String executeSQL(String[] split){
    SessionFactory hibernateFactory = someService.getHibernateFactory();
    String message = null;
    int rows = 0;
    EntityManager entityManager = hibernateFactory.createEntityManager();
    EntityTransaction utx = entityManager.getTransaction();
    for (int i = 0; i < split.length; i++) {
        String query = split[i];
        try {
            utx.begin();
            Query query1 = entityManager.createNativeQuery(query);
            rows = query1.executeUpdate();
            utx.commit();
            try{
                resultList = query1.getResultList();
                resultList.stream().map(Arrays::toString).forEach(System.out::println);
            }
            catch(Exception e){
            }
            message = "Success \n{ ["+rows+"] <--- affected rows}";
        }catch (PersistenceException e){
            utx.rollback();
            message = (((SQLGrammarException)e.getCause()).getSQLException()).getMessage();
        }
    }
    entityManager.close();
    return message;
}

I know how to save values into list as you can see but i can't extract columns... because there is no information about columns name. In that logs columns have names and also columns have type, that informations would be usefull for me.

As an option how it could be implemented is Logback Appender Once you will have logs into appender you can implement filtering and whatever you need.

1.Add dependency to logback-classic

<dependency>
   <groupId>ch.qos.logback</groupId>
   <artifactId>logback-classic</artifactId>
   <version>1.2.3</version>
</dependency>

2.Add logback.xml to resources

<configuration>
    <appender name="list" class="com.test.ListAppender"/>
    <root level="info">
        <appender-ref ref="list"/>
    </root>
</configuration>

3.Implement Appender class

package com.test;

import ch.qos.logback.classic.spi.ILoggingEvent;
import ch.qos.logback.core.AppenderBase;

import java.util.LinkedList;
import java.util.List;

public class ListAppender extends AppenderBase<ILoggingEvent> {

    private static List<ILoggingEvent> eventList = new LinkedList<>();

    @Override
    protected void append(ILoggingEvent event) {
        eventList.add(event);
    }

    public List<ILoggingEvent> getEventList() {
        return eventList;
    }
}

4.Test your code

package com.test;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

public class Main {
    private static final Logger logger = LoggerFactory.getLogger(Main.class);

    static ListAppender listAppender = new ListAppender();

    public static void main(String[] args) {
        logger.info("Hello World");
        listAppender.getEventList().forEach(entry -> System.out.println("print log from list:" + entry));
        System.out.println();
    }
}
Related
Is Java “pass-by-reference” or “pass-by-value”?
How do I efficiently iterate over each entry in a Java Map?
Does a finally block always get executed in Java?
How do I call one constructor from another in Java?
How do I read / convert an InputStream into a String in Java?
How do I generate random integers within a specific range in Java?
How to get an enum value from a string value in Java?
How do I determine whether an array contains a particular value in Java?
How do I convert a String to an int in Java?
How to create a memory leak in Java?