Skip to content

Commit

Permalink
Merge pull request #11 from gpu7/dev
Browse files Browse the repository at this point in the history
Dev
  • Loading branch information
gpu7 authored May 23, 2024
2 parents 68d1613 + 943b544 commit caf3f22
Show file tree
Hide file tree
Showing 3 changed files with 55 additions and 23 deletions.
11 changes: 11 additions & 0 deletions .dockerignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
__pycache__
*.pyc
*.pyo
*.pyd
*.pdb
*.egg
*.egg-info
dist
build
.vscode
.idea
20 changes: 20 additions & 0 deletions Dockerfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
# Use the official Python image from the Docker Hub
FROM python:3.12.3-slim

# Set the working directory in the container
WORKDIR /app

# Copy the requirements file into the container
COPY requirements.txt .

# Install the dependencies
RUN pip install --no-cache-dir -r requirements.txt

# Copy the rest of the application code into the container
COPY . .

# Set environment variable to indicate Docker environment
ENV DOCKER_ENV=1

# Specify the command to run the application
CMD ["python", "VoterRoll.py"]
47 changes: 24 additions & 23 deletions utilities/loggerUtilVoterRoll.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,8 @@
import os
import platform
import sys
from logging.handlers import RotatingFileHandler
from pythonjsonlogger import jsonlogger
from logging.handlers import RotatingFileHandler
from pythonjsonlogger import jsonlogger

# set logger name = module name
logger = logging.getLogger(__name__)
Expand All @@ -23,27 +23,28 @@
sys.exit(1)

# File handler. date format is ISO-8601
# rotate and delete log files
try:
# Identify the platform
if platform.system() == 'Windows': # Windows
logs_file = os.path.join(os.environ['USERPROFILE'], 'VoterRoll', 'logs', 'logs.txt')
else:
raise ValueError("ERROR: unsupported platform")

fileHandler = RotatingFileHandler(logs_file, backupCount=5, maxBytes=50000000) # five log file rotations; rotate log filename at 50 Mb; delete oldest file

fmtJson = jsonlogger.JsonFormatter(
"%(name)s %(asctime)s %(levelname)s %(filename)s %(lineno)s %(process)d %(message)s",
rename_fields={"levelname": "severity", "asctime": "timestamp"},
datefmt="%Y-%m-%dT%H:%M:%SZ",
) # json file format
fileHandler.setFormatter(fmtJson)
fileHandler.setLevel(logging.DEBUG) # set log level (DEBUG, INFO, WARNING, ERROR, CRITICAL)
logger.addHandler(fileHandler)
except Exception as e:
print(f"ERROR: failed to set up file logging: {e}")
sys.exit(1)
# Only add file logging if not running inside Docker
if not os.environ.get('DOCKER_ENV'):
try:
# Identify the platform
if platform.system() == 'Windows': # Windows
logs_file = os.path.join(os.environ['USERPROFILE'], 'VoterRoll', 'logs', 'logs.txt')
else:
raise ValueError("ERROR: unsupported platform")

fileHandler = RotatingFileHandler(logs_file, backupCount=5, maxBytes=50000000) # five log file rotations; rotate log filename at 50 Mb; delete oldest file

fmtJson = jsonlogger.JsonFormatter(
"%(name)s %(asctime)s %(levelname)s %(filename)s %(lineno)s %(process)d %(message)s",
rename_fields={"levelname": "severity", "asctime": "timestamp"},
datefmt="%Y-%m-%dT%H:%M:%SZ",
) # json file format
fileHandler.setFormatter(fmtJson)
fileHandler.setLevel(logging.DEBUG) # set log level (DEBUG, INFO, WARNING, ERROR, CRITICAL)
logger.addHandler(fileHandler)
except Exception as e:
print(f"ERROR: failed to set up file logging: {e}")
sys.exit(1)

# Uncaught exceptions handler.
# log these as CRITICAL. KeyboardInterrupt is treated as normal termination of the script.
Expand Down

0 comments on commit caf3f22

Please sign in to comment.