Change trace file in H2 - h2

My application uses H2 but already has a log file (ex: abc.log)
Now, I'm trying to make even the H2 to write logs/errors to that file (abc.log) so if something goes wrong an user has only 1 file to send to me (not abc.log AND abc.db.trace file)
Is there a way to achieve that?

You can configure H2 to use SL4FJ as follows:
jdbc:h2:~/test;TRACE_LEVEL_FILE=4
The logger name is h2database.

Ok the solution was to simple for me to believe it but the only thing I had to do is to add
slf4j-api-1.7.2.jar
and
slf4j-jdk14-1.7.2.jar
in my app's classpath.
As SLF4J will (first search and then) discover by itself what underlying logging framework to use it is simply a matter of placing the right implementation.
One warning, it seems that SLF4J can not use more than one frameworks at a time so this solution work ONLY if you have a single existing framework.

Related

Springboot Logging Pattern For Rolling Logs

I am using Springboot version 2.7 and trying to configure the log pattern to be daily rolling.
I am currently using just the application properties file to configure the logging as that's the preference.
I added the following line in the properties file but does not seem to work
logging.logback.rollingpolicy.file-name-pattern=myservice-%d{yyyy-MM-dd}.log
Any clues what I may be missing?
Also, is there a way to check daily log rolling without having to wait for EOD :)
First, you have to specify the file name:
logging.file.name=myservice.log
then you can use the rolling file name pattern
logging.logback.rollingpolicy.file-name-pattern=myservice-%d{yyyy-MM-dd}.log
To force the file change you could set the size to something small
logging.logback.rollingpolicy.max-file-size=100K
To specify the directory you must set this property
logging.file.path=/var/logs
The documentation can be found here:
https://docs.spring.io/spring-boot/docs/current/reference/html/features.html#features.logging

Best way to concatenate/collapse stacktraces in logs

My goal is to include my stacktrace and log message into a single log message for my Spring Boot applications. The problem I'm running into is each line of the stacktrace is a separate log message. I want to be able to search my logs for a log level of ERROR and find the log message with the stacktrace. I've found two solutions but not sure which to use.
I can use Logback to put them all in one line but would like to keep the new lines for a pretty format. Also the guide I found might override defaults that I want to keep. https://fabianlee.org/2018/03/09/java-collapsing-multiline-stack-traces-into-a-single-log-event-using-spring-backed-by-logback-or-log4j2/
I could also use ECS and concatenate it there, but it could affect other logs (though I think we only have Java apps). https://docs.aws.amazon.com/AmazonECS/latest/developerguide/firelens-concatanate-multiline.html
Which would be the best way to do it? Also is there a better way to do it in Spring compared to the guide that I found?

Access sqlite db file with jpa

I have a huge sqlite file containing my db. I need to know if it is possible and how to connect to this db as an embedded one with jpa.
I'm developing an app that packs this database inside it's own jar so that when I use it on another system I don't have to import a copy of my db back and forth.
The technologies I'd like to use are Angular and Spring since those are the ones I know best. If there are some techonlogies that better suit this purpose I'd like some suggestions.
Thanks :)
I hope I undestood your question correctly, so I made a small project for you, hence you can have a look into it: spring-jpa-sqlite-sample. It may guide you a bit, though I and don't claim correctness or completeness.
The path to the sqlite file can easily be changed by inserting the correct url in the persistence.properties file:
driverClassName=org.sqlite.JDBC
url=jdbc:sqlite:src/main/resources/chinook.db --> you may use relative paths.
hibernate.dialect=dev.mutiny.semo.config.SQLiteDataTypesConfig
hibernate.hbm2ddl.auto=none
hibernate.show_sql=true
You can also use Environment variables from your system, which Spring tries to read from, so that you can reference the correct directory to a file. This can be found here: Read system environment var (SO)
Last but not least. Beware of using huge SQLite files. Find another way and transfer it first into a 'real' Database like any other Client/Server RDBMS you know (Oracle, MariaDB, MSSQL, depends on your scenario/taste).
Have closer look onto the documentation: When to use SQLite (and when not to!)

Spring batch integration file lock access

I have a spring batch integration where multiple servers are polling a single file directory. This causes a problem where a file can be processed up by more than one. I have attempted to add a nio-lock onto the file once a server has got it but this locks the file for processing so it can't read the contents of the file.
Is there a spring batch/integration solution to this problem or is there a way to rename the file as soon as it is picked up by a node?
Consider to use FileSystemPersistentAcceptOnceFileListFilter with the shared MetadataStore: http://docs.spring.io/spring-integration/reference/html/system-management-chapter.html#metadata-store
So, only one instance of your application will be able to pick up a file.
Even if we find a solution for nio-lock, you should understand that lock means "do not touch until freed". Therefore when one instance has done its work, another one is ready to pick up the file. I guess that isn't your goal.

Can I dynamically update the websphere 's log configuration in my own code?

In websphere admin console, we can change the log configuration , for example, the log file name, rolloverSize and log level and so on. After we saved the change in console, if we choose run time and chose write the file to xml. In this case, the log configuraiton change will be write to config file (such as: \config\cells\SENLI-XPNode01Cell\nodes\SENLI-XPNode01\servers\server1\server.xml)
You know websphere support the dynamically change the log configuraiton, after we changed it, it will take effect immidiately.
My quesiton is that in my own code, is there any websphere public api we can invoke to implement the dynamically change the log configuraiton?
You use the AdminClient to look up the TraceService MBean and update the trace string via that interface.
To modify the values saved in xml you can use the ConfigService to programmatically. I'll warn you that this approach isn't for the faint of heart.

Resources