T
T
Tidus2022-02-20 13:27:21
Database
Tidus, 2022-02-20 13:27:21

How to properly log a database?

Is a separate table for logs made, or what?
Tell us more about how it is now accepted in production.

Answer the question

In order to leave comments, you need to log in

4 answer(s)
O
Orkhan Hasanli, 2022-02-20
@azerphoenix

To be honest, it's not entirely clear what you want.
The database has logging. Here, for example, PostgreSQL
If we are talking about hibernate & jpa logs, then here is a useful article:
https://www.baeldung.com/sql-logging-spring-boot
You can set the logging level, and then, for example, output the logs to a file
If we are talking about Spring & Hibernate (about listening), for example, when which entity was added or updated, that is, for example, EntityListeners.
Here is a simple example:

@SpringBootApplication
@EnableJpaAuditing
public class Application {
  public static void main(String[] args) {
    SpringApplication.run(Application.class, args);
  }
}

entity User -
@Entity
@EntityListeners(AuditingEntityListener.class)
@Table(name = "users")
public class User {

    @Id
    @GeneratedValue(strategy = GenerationType.SEQUENCE)
    private Long id;

    @CreatedDate
    @Column(updatable = false)
    private LocalDateTime created;

    @LastModifiedDate
    private LocalDateTime lastModified;

}

M
Michael, 2022-02-20
@Akela_wolf

To log in a DB (in the separate table) the spring quite can. But this is usually not a very good solution. In industrial use, it is still better to log to a file, then logstash -> elastic -> grafana/kibana or a similar stack. You can graylog - in general, there are a lot of solutions and it usually makes little sense to load a relational database with logs.

V
Vladimir Korotenko, 2022-02-20
@firedragon

Decide what you want to log.
1. the level of the database event is already dropped into the system log
2. Schema changes, depends on the DBMS and is configured in some
3. Data change, just add the user to a separate field
4. Versioning, the same as in pp3 but the version and changed columns ( strongly do not advise)
5. Blame trigger for some actions, written in a separate table, it all depends on the business
Something else. Describe what you want in the context of the business, just explain how stupid. Where, who, why and what

N
Nikolay Savelyev, 2022-02-20
@AgentSmith

The database itself already has built-in logging

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question