Subject: Feature #6395 - Basic Log Rotation

From: Thomas Greenwood <[email-masked]>
Date: Fri, 16 Apr 2021 18:29:34 +0100

Hi,

I've opened feature issue #6395 to enable log rotation in pgAdmin4 v5.

Please find the attached basic patch as a starter for 10. It's the same 
patch attached to the feature issue.

Thanks a lot,

Tom

diff -Naur orig/__init__.py new/__init__.py
--- orig/__init__.py	2021-04-16 17:22:51.816949501 +0100
+++ new/__init__.py	2021-04-16 17:24:46.583081282 +0100
@@ -10,6 +10,7 @@
 """The main pgAdmin module. This handles the application initialisation tasks,
 such as setup of logging, dynamic loading of modules etc."""
 import logging
+from logging.handlers import RotatingFileHandler
 import os
 import sys
 import re
@@ -254,11 +255,17 @@
         # Ensure the various working directories exist
         from pgadmin.setup import create_app_data_directory
         create_app_data_directory(config)
+        
+        if config.LOG_ROTATION_MAX_BYTES and config.LOG_ROTATION_NUM_BACKUPS:
+                # Rotated File logging
+                fh = RotatingFileHandler(config.LOG_FILE, mode='a', maxBytes=config.LOG_ROTATION_MAX_BYTES, backupCount=config.LOG_ROTATION_NUM_BACKUPS, encoding='utf-8', delay=False)
+        else: 
+                # File logging
+                fh = logging.FileHandler(config.LOG_FILE, encoding='utf-8')
 
-        # File logging
-        fh = logging.FileHandler(config.LOG_FILE, encoding='utf-8')
         fh.setLevel(config.FILE_LOG_LEVEL)
         fh.setFormatter(logging.Formatter(config.FILE_LOG_FORMAT))
+
         app.logger.addHandler(fh)
         logger.addHandler(fh)

---

From: COG Internet <[email-masked]>
Date: Mon, 19 Apr 2021 17:31:45 +0100

Hi,

I have a version 2 of this with a fixed conditional and initial config 
values which I'll send through later today.

Thanks,

Tom

---

From: Dave Page <[email-masked]>
Date: Tue, 20 Apr 2021 11:23:39 +0100

Hi

> patch attached to the feature issue.

Nice! My only suggestions would be that the config options be added to
config.py, with default values of zero for log size, and say, 5 for the
number of backups (should that be 'archives'?), and that we test to see if
the log size is > 0 before enabling rotation. The reason for that is that
we keep all config options in config.py to ensure it's self-documenting.

-- 
Dave Page
Blog: https://pgsnake.blogspot.com
Twitter: @pgsnake

EDB: https://www.enterprisedb.com

---

From: Akshay Joshi <[email-masked]>
Date: Thu, 29 Apr 2021 12:22:16 +0530

Hi Tom

> values which I'll send through later today.

    Have you worked on this? If patch is ready can you please send it.

-- 
*Thanks & Regards*
*Akshay Joshi*
*pgAdmin Hacker | Principal Software Architect*
*EDB Postgres <http://edbpostgres.com>*

*Mobile: +91 976-788-8246*