public inbox for [email protected]  
help / color / mirror / Atom feed
From: Yogesh Mahajan <[email protected]>
To: pgadmin-hackers <[email protected]>
Subject: [pgAdmin][Patch] - Feature #6395 - Feature request: Log Rotation
Date: Tue, 25 May 2021 10:53:34 +0530
Message-ID: <CAMa=N=OAkNDvwX+c6VA1+9PDBtdHt+fTYKm8nwm8jt-BOh=9BQ@mail.gmail.com> (raw)

Hi,

Please find the attached patch which rotates the pgadmin log file with
context to the parameters specified in the config file.

# Log rotation setting
LOG_ROTATION_SIZE = 10  # In MBs
LOG_ROTATION_AGE = 1440  # In minutes
LOG_ROTATION_MAX_LOGS = 90  # Maximum number of backups to retain


Thanks,
Yogesh Mahajan
EnterpriseDB


Attachments:

  [application/x-patch] RM6395_v1.patch (3.8K, 3-RM6395_v1.patch)
  download | inline diff:
diff --git a/web/config.py b/web/config.py
index 4d1f98698..663788e50 100644
--- a/web/config.py
+++ b/web/config.py
@@ -286,6 +286,10 @@ if SERVER_MODE and not IS_WIN:
 else:
     LOG_FILE = os.path.join(DATA_DIR, 'pgadmin4.log')
 
+# Log rotation setting
+LOG_ROTATION_SIZE = 10  # In MBs
+LOG_ROTATION_AGE = 1440  # In minutes
+LOG_ROTATION_MAX_LOGS = 90  # Maximum number of backups to retain
 ##########################################################################
 # Server Connection Driver Settings
 ##########################################################################
diff --git a/web/pgadmin/__init__.py b/web/pgadmin/__init__.py
index a73335371..6449806f2 100644
--- a/web/pgadmin/__init__.py
+++ b/web/pgadmin/__init__.py
@@ -255,8 +255,14 @@ def create_app(app_name=None):
         from pgadmin.setup import create_app_data_directory
         create_app_data_directory(config)
 
-        # File logging
-        fh = logging.FileHandler(config.LOG_FILE, encoding='utf-8')
+        # File logging # Rotated File logging
+        from pgadmin.utils.enhanced_log_rotation import \
+            EnhancedRotatingFileHandler
+        fh = EnhancedRotatingFileHandler(config.LOG_FILE,
+                                         config.LOG_ROTATION_SIZE,
+                                         config.LOG_ROTATION_AGE,
+                                         config.LOG_ROTATION_MAX_LOGS)
+
         fh.setLevel(config.FILE_LOG_LEVEL)
         fh.setFormatter(logging.Formatter(config.FILE_LOG_FORMAT))
         app.logger.addHandler(fh)
diff --git a/web/pgadmin/utils/enhanced_log_rotation.py b/web/pgadmin/utils/enhanced_log_rotation.py
new file mode 100644
index 000000000..b46c53420
--- /dev/null
+++ b/web/pgadmin/utils/enhanced_log_rotation.py
@@ -0,0 +1,46 @@
+##########################################################################
+#
+# pgAdmin 4 - PostgreSQL Tools
+#
+# Copyright (C) 2013 - 2021, The pgAdmin Development Team
+# This software is released under the PostgreSQL Licence
+#
+##########################################################################
+
+import re
+import logging.handlers as handlers
+
+
+class EnhancedRotatingFileHandler(handlers.TimedRotatingFileHandler,
+                                  handlers.RotatingFileHandler):
+    """
+    Handler for logging to a set of files, which switches from one file
+    to the next when the current file reaches a certain size, or at certain
+    timed intervals
+    """
+    def __init__(self, filename, max_bytes=1, interval=60, backup_count=0,
+                 encoding=None, when='M'):
+        max_bytes = max_bytes * 1000000
+        handlers.TimedRotatingFileHandler.__init__(self, filename=filename,
+                                                   when=when,
+                                                   interval=interval,
+                                                   backupCount=backup_count,
+                                                   encoding=encoding)
+
+        handlers.RotatingFileHandler.__init__(self, filename=filename,
+                                              mode='a',
+                                              maxBytes=max_bytes,
+                                              backupCount=backup_count,
+                                              encoding=encoding)
+
+    # Time & Size combined rollover
+    def shouldRollover(self, record):
+        return handlers.TimedRotatingFileHandler.shouldRollover(self, record) \
+            or handlers.RotatingFileHandler.shouldRollover(self, record)
+
+    # Roll overs current file
+    def doRollover(self):
+        self.suffix = "%Y-%m-%d_%H-%M-%S"
+        self.extMatch = r"^\d{4}-\d{2}-\d{2}_\d{2}-\d{2}-\d{2}(\.\w+)?$"
+        self.extMatch = re.compile(self.extMatch, re.ASCII)
+        handlers.TimedRotatingFileHandler.doRollover(self)


view thread (4+ messages)  latest in thread

reply

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Reply to all the recipients using the --to and --cc options:
  reply via email

  To: [email protected]
  Cc: [email protected]
  Subject: Re: [pgAdmin][Patch] - Feature #6395 - Feature request: Log Rotation
  In-Reply-To: <CAMa=N=OAkNDvwX+c6VA1+9PDBtdHt+fTYKm8nwm8jt-BOh=9BQ@mail.gmail.com>

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

This inbox is served by agora; see mirroring instructions
for how to clone and mirror all data and code used for this inbox