feat: rework logging and rotation rules
This commit is contained in:
@@ -26,3 +26,26 @@ postgres_effective_cache_size: 1GB
|
||||
postgres_maintenance_work_mem: 64MB
|
||||
postgres_work_mem: 4MB
|
||||
postgres_max_connections: 100
|
||||
|
||||
# Logging configuration
|
||||
# Backend: 'journald' (systemd journal) or 'file' (traditional log files)
|
||||
postgres_log_backend: journald
|
||||
|
||||
# Logging settings (apply to both backends)
|
||||
postgres_log_min_duration_statement: -1 # -1 disables, 0 logs all, >0 logs slow queries (ms)
|
||||
postgres_log_line_prefix: "%m [%p] %q%u@%d " # timestamp [pid] user@database
|
||||
postgres_log_timezone: UTC
|
||||
|
||||
# File backend settings (only used when postgres_log_backend: file)
|
||||
postgres_log_destination: stderr # stderr|csvlog|syslog
|
||||
postgres_logging_collector: "on" # Enable log file collection
|
||||
postgres_log_directory: log # Relative to data directory
|
||||
postgres_log_filename: postgresql-%Y-%m-%d_%H%M%S.log
|
||||
postgres_log_rotation_age: 1d # Rotate after this time (0 disables)
|
||||
postgres_log_rotation_size: 100MB # Rotate after this size (0 disables)
|
||||
postgres_log_truncate_on_rotation: "off" # Overwrite old log files with same name
|
||||
|
||||
# Logrotate configuration (only used when postgres_log_backend: file)
|
||||
postgres_logrotate_rotate: 14 # Keep 14 days of logs
|
||||
postgres_logrotate_frequency: daily # daily|weekly|monthly
|
||||
postgres_logrotate_compress: true # Compress rotated logs
|
||||
|
||||
@@ -73,6 +73,21 @@
|
||||
enabled: true
|
||||
state: started
|
||||
|
||||
- name: Deploy logrotate configuration for PostgreSQL
|
||||
ansible.builtin.template:
|
||||
src: logrotate-postgresql.j2
|
||||
dest: /etc/logrotate.d/postgresql
|
||||
owner: root
|
||||
group: root
|
||||
mode: "0644"
|
||||
when: postgres_log_backend == 'file'
|
||||
|
||||
- name: Remove logrotate configuration when using journald
|
||||
ansible.builtin.file:
|
||||
path: /etc/logrotate.d/postgresql
|
||||
state: absent
|
||||
when: postgres_log_backend == 'journald'
|
||||
|
||||
- name: Set PostgreSQL admin user password
|
||||
community.postgresql.postgresql_user:
|
||||
name: "{{ postgres_admin_user }}"
|
||||
|
||||
@@ -11,3 +11,26 @@ effective_cache_size = {{ postgres_effective_cache_size }}
|
||||
maintenance_work_mem = {{ postgres_maintenance_work_mem }}
|
||||
work_mem = {{ postgres_work_mem }}
|
||||
max_connections = {{ postgres_max_connections }}
|
||||
|
||||
# Logging configuration
|
||||
{% if postgres_log_backend == 'journald' %}
|
||||
# Log to systemd journal via stderr (journald captures it automatically)
|
||||
log_destination = 'stderr'
|
||||
logging_collector = off
|
||||
{% else %}
|
||||
# Log to files
|
||||
log_destination = '{{ postgres_log_destination }}'
|
||||
logging_collector = {{ postgres_logging_collector }}
|
||||
log_directory = '{{ postgres_log_directory }}'
|
||||
log_filename = '{{ postgres_log_filename }}'
|
||||
log_rotation_age = {{ postgres_log_rotation_age }}
|
||||
log_rotation_size = {{ postgres_log_rotation_size }}
|
||||
log_truncate_on_rotation = {{ postgres_log_truncate_on_rotation }}
|
||||
{% endif %}
|
||||
|
||||
# Logging details (applies to both backends)
|
||||
log_line_prefix = '{{ postgres_log_line_prefix }}'
|
||||
log_timezone = '{{ postgres_log_timezone }}'
|
||||
{% if postgres_log_min_duration_statement >= 0 %}
|
||||
log_min_duration_statement = {{ postgres_log_min_duration_statement }}
|
||||
{% endif %}
|
||||
|
||||
Reference in New Issue
Block a user