mirror of
https://github.com/ep1cman/unifi-protect-backup.git
synced 2025-12-11 05:43:18 +00:00
color logging no longer uses global variable
This commit is contained in:
@@ -19,9 +19,6 @@ from unifi_protect_backup.utils import (
|
|||||||
setup_event_logger,
|
setup_event_logger,
|
||||||
)
|
)
|
||||||
|
|
||||||
logger = logging.getLogger(__name__)
|
|
||||||
setup_event_logger(logger)
|
|
||||||
|
|
||||||
|
|
||||||
async def get_video_length(video: bytes) -> float:
|
async def get_video_length(video: bytes) -> float:
|
||||||
"""Uses ffprobe to get the length of the video file passed in as a byte stream"""
|
"""Uses ffprobe to get the length of the video file passed in as a byte stream"""
|
||||||
@@ -39,13 +36,18 @@ async def get_video_length(video: bytes) -> float:
|
|||||||
class VideoDownloader:
|
class VideoDownloader:
|
||||||
"""Downloads event video clips from Unifi Protect"""
|
"""Downloads event video clips from Unifi Protect"""
|
||||||
|
|
||||||
def __init__(self, protect: ProtectApiClient, download_queue: asyncio.Queue, upload_queue: VideoQueue):
|
def __init__(
|
||||||
|
self, protect: ProtectApiClient, download_queue: asyncio.Queue, upload_queue: VideoQueue, color_logging: bool
|
||||||
|
):
|
||||||
self._protect: ProtectApiClient = protect
|
self._protect: ProtectApiClient = protect
|
||||||
self.download_queue: asyncio.Queue = download_queue
|
self.download_queue: asyncio.Queue = download_queue
|
||||||
self.upload_queue: VideoQueue = upload_queue
|
self.upload_queue: VideoQueue = upload_queue
|
||||||
self.logger = logging.LoggerAdapter(logger, {'event': ''})
|
|
||||||
self.current_event = None
|
self.current_event = None
|
||||||
|
|
||||||
|
self.base_logger = logging.getLogger(__name__)
|
||||||
|
setup_event_logger(self.base_logger, color_logging)
|
||||||
|
self.logger = logging.LoggerAdapter(self.base_logger, {'event': ''})
|
||||||
|
|
||||||
# Check if `ffprobe` is available
|
# Check if `ffprobe` is available
|
||||||
ffprobe = shutil.which('ffprobe')
|
ffprobe = shutil.which('ffprobe')
|
||||||
if ffprobe is not None:
|
if ffprobe is not None:
|
||||||
@@ -61,7 +63,7 @@ class VideoDownloader:
|
|||||||
try:
|
try:
|
||||||
event = await self.download_queue.get()
|
event = await self.download_queue.get()
|
||||||
self.current_event = event
|
self.current_event = event
|
||||||
self.logger = logging.LoggerAdapter(logger, {'event': f' [{event.id}]'})
|
self.logger = logging.LoggerAdapter(self.base_logger, {'event': f' [{event.id}]'})
|
||||||
|
|
||||||
# Fix timezones since pyunifiprotect sets all timestamps to UTC. Instead localize them to
|
# Fix timezones since pyunifiprotect sets all timestamps to UTC. Instead localize them to
|
||||||
# the timezone of the unifi protect NVR.
|
# the timezone of the unifi protect NVR.
|
||||||
|
|||||||
@@ -99,7 +99,8 @@ class UnifiProtectBackup:
|
|||||||
for notifier in apprise_notifiers:
|
for notifier in apprise_notifiers:
|
||||||
notifications.add_notification_service(notifier)
|
notifications.add_notification_service(notifier)
|
||||||
|
|
||||||
setup_logging(verbose, color_logging)
|
self.color_logging = color_logging
|
||||||
|
setup_logging(verbose, self.color_logging)
|
||||||
|
|
||||||
logger.debug("Config:")
|
logger.debug("Config:")
|
||||||
logger.debug(f" {address=}")
|
logger.debug(f" {address=}")
|
||||||
@@ -204,7 +205,7 @@ class UnifiProtectBackup:
|
|||||||
|
|
||||||
# Create downloader task
|
# Create downloader task
|
||||||
# This will download video files to its buffer
|
# This will download video files to its buffer
|
||||||
downloader = VideoDownloader(self._protect, download_queue, upload_queue)
|
downloader = VideoDownloader(self._protect, download_queue, upload_queue, self.color_logging)
|
||||||
tasks.append(asyncio.create_task(downloader.start()))
|
tasks.append(asyncio.create_task(downloader.start()))
|
||||||
|
|
||||||
# Create upload task
|
# Create upload task
|
||||||
@@ -216,6 +217,7 @@ class UnifiProtectBackup:
|
|||||||
self.rclone_args,
|
self.rclone_args,
|
||||||
self.file_structure_format,
|
self.file_structure_format,
|
||||||
self._db,
|
self._db,
|
||||||
|
self.color_logging,
|
||||||
)
|
)
|
||||||
tasks.append(asyncio.create_task(uploader.start()))
|
tasks.append(asyncio.create_task(uploader.start()))
|
||||||
|
|
||||||
|
|||||||
@@ -10,9 +10,6 @@ from pyunifiprotect import ProtectApiClient
|
|||||||
|
|
||||||
from unifi_protect_backup.utils import get_camera_name, VideoQueue, run_command, setup_event_logger, human_readable_size
|
from unifi_protect_backup.utils import get_camera_name, VideoQueue, run_command, setup_event_logger, human_readable_size
|
||||||
|
|
||||||
logger = logging.getLogger(__name__)
|
|
||||||
setup_event_logger(logger)
|
|
||||||
|
|
||||||
|
|
||||||
class VideoUploader:
|
class VideoUploader:
|
||||||
"""Uploads videos from the video_queue to the provided rclone destination
|
"""Uploads videos from the video_queue to the provided rclone destination
|
||||||
@@ -28,6 +25,7 @@ class VideoUploader:
|
|||||||
rclone_args: str,
|
rclone_args: str,
|
||||||
file_structure_format: str,
|
file_structure_format: str,
|
||||||
db: aiosqlite.Connection,
|
db: aiosqlite.Connection,
|
||||||
|
color_logging: bool,
|
||||||
):
|
):
|
||||||
self._protect: ProtectApiClient = protect
|
self._protect: ProtectApiClient = protect
|
||||||
self.upload_queue: VideoQueue = upload_queue
|
self.upload_queue: VideoQueue = upload_queue
|
||||||
@@ -35,9 +33,12 @@ class VideoUploader:
|
|||||||
self._rclone_args: str = rclone_args
|
self._rclone_args: str = rclone_args
|
||||||
self._file_structure_format: str = file_structure_format
|
self._file_structure_format: str = file_structure_format
|
||||||
self._db: aiosqlite.Connection = db
|
self._db: aiosqlite.Connection = db
|
||||||
self.logger = logging.LoggerAdapter(logger, {'event': ''})
|
|
||||||
self.current_event = None
|
self.current_event = None
|
||||||
|
|
||||||
|
self.base_logger = logging.getLogger(__name__)
|
||||||
|
setup_event_logger(self.base_logger, color_logging)
|
||||||
|
self.logger = logging.LoggerAdapter(self.base_logger, {'event': ''})
|
||||||
|
|
||||||
async def start(self):
|
async def start(self):
|
||||||
"""Main loop
|
"""Main loop
|
||||||
|
|
||||||
@@ -50,7 +51,7 @@ class VideoUploader:
|
|||||||
event, video = await self.upload_queue.get()
|
event, video = await self.upload_queue.get()
|
||||||
self.current_event = event
|
self.current_event = event
|
||||||
|
|
||||||
self.logger = logging.LoggerAdapter(logger, {'event': f' [{event.id}]'})
|
self.logger = logging.LoggerAdapter(self.base_logger, {'event': f' [{event.id}]'})
|
||||||
|
|
||||||
self.logger.info(f"Uploading event: {event.id}")
|
self.logger.info(f"Uploading event: {event.id}")
|
||||||
self.logger.debug(
|
self.logger.debug(
|
||||||
|
|||||||
@@ -152,11 +152,11 @@ class AppriseStreamHandler(logging.StreamHandler):
|
|||||||
self.handleError(record)
|
self.handleError(record)
|
||||||
|
|
||||||
|
|
||||||
def create_logging_handler(format):
|
|
||||||
|
def create_logging_handler(format, color_logging):
|
||||||
date_format = "%Y-%m-%d %H:%M:%S"
|
date_format = "%Y-%m-%d %H:%M:%S"
|
||||||
style = '{'
|
style = '{'
|
||||||
|
|
||||||
global color_logging
|
|
||||||
sh = AppriseStreamHandler(color_logging)
|
sh = AppriseStreamHandler(color_logging)
|
||||||
formatter = logging.Formatter(format, date_format, style)
|
formatter = logging.Formatter(format, date_format, style)
|
||||||
sh.setFormatter(formatter)
|
sh.setFormatter(formatter)
|
||||||
@@ -183,8 +183,6 @@ def setup_logging(verbosity: int, color_logging: bool = False, apprise_notifiers
|
|||||||
apprise_notifiers (List[str]): Notification services to hook into the logger
|
apprise_notifiers (List[str]): Notification services to hook into the logger
|
||||||
|
|
||||||
"""
|
"""
|
||||||
globals()['color_logging'] = color_logging
|
|
||||||
|
|
||||||
add_logging_level(
|
add_logging_level(
|
||||||
'EXTRA_DEBUG',
|
'EXTRA_DEBUG',
|
||||||
logging.DEBUG - 1,
|
logging.DEBUG - 1,
|
||||||
@@ -195,7 +193,7 @@ def setup_logging(verbosity: int, color_logging: bool = False, apprise_notifiers
|
|||||||
)
|
)
|
||||||
|
|
||||||
format = "{asctime} [{levelname:^11s}] {name:<42} : {message}"
|
format = "{asctime} [{levelname:^11s}] {name:<42} : {message}"
|
||||||
sh = create_logging_handler(format)
|
sh = create_logging_handler(format, color_logging)
|
||||||
|
|
||||||
logger = logging.getLogger("unifi_protect_backup")
|
logger = logging.getLogger("unifi_protect_backup")
|
||||||
logger.addHandler(sh)
|
logger.addHandler(sh)
|
||||||
@@ -221,9 +219,9 @@ def setup_logging(verbosity: int, color_logging: bool = False, apprise_notifiers
|
|||||||
logger.setLevel(logging.WEBSOCKET_DATA) # type: ignore
|
logger.setLevel(logging.WEBSOCKET_DATA) # type: ignore
|
||||||
|
|
||||||
|
|
||||||
def setup_event_logger(logger):
|
def setup_event_logger(logger, color_logging):
|
||||||
format = "{asctime} [{levelname:^11s}] {name:<42} :{event} {message}"
|
format = "{asctime} [{levelname:^11s}] {name:<42} :{event} {message}"
|
||||||
sh = create_logging_handler(format)
|
sh = create_logging_handler(format, color_logging)
|
||||||
logger.addHandler(sh)
|
logger.addHandler(sh)
|
||||||
logger.propagate = False
|
logger.propagate = False
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user