Take the 2-minute tour ×
Stack Overflow is a question and answer site for professional and enthusiast programmers. It's 100% free, no registration required.

I've got web application using Spring. To connect with my database I'm using Hibernate. I've got requirement to log all SQL queries executed by application users (something like database logs per application user). What I am doing now is very bad - I am parsing log4j logs and puts them into database with application user as author. I was wondering if is there any possibility to achieve this by database logging-system? The problem is that I am connecting to ma database via Hibernate with single user with name postgres - so is it possible to make some magic and tell (via Hibernate/Spring/whatever) database logging-system that this SQL is executing user with login XXX (not user with login postgres)? There is no need, to browse this logs via web application - it is good enough to use database logging-system and browse it with native tools (like pgAdmin).

Any ideas?

Thanks, Arek

share|improve this question

1 Answer 1

If statement logging is turned on, the complete statement (that means including comments) will be logged.

SELECT /* user John */ * from pg_tables;

The next step is to find the spots in Hibernate where the statements are created. If you are lucky, this is limited to two relevant locations, one for SELECTs and one for insert/update/delete etc.

To pass down the login information into Hibernate, you could try a ThreadLocal variable.

Not really easy.

Another idea: PostgreSql 9.1 supports "set application_name to ..." together with java.sql.Connection.setClientInfo. Maybe the statement logging can be configured to show this value together with the statement.

Is it that "bad" to parse your log files?

share|improve this answer
    
thanks for the answer! No, this is not 'bad' to parse my log files, but I was wondering if there is more clever solution. Thanks for suggestions, but I think I will stay with log files parsing (less troubles) :) –  Arek Woźniak May 17 '13 at 6:19

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.