Small cleanup and add FileLogger
This commit is contained in:
@@ -158,3 +158,5 @@ cython_debug/
|
||||
# and can be added to the global gitignore or merged into this file. For a more nuclear
|
||||
# option (not recommended) you can uncomment the following to ignore the entire idea folder.
|
||||
.idea/
|
||||
|
||||
uv.lock
|
||||
|
||||
@@ -0,0 +1,52 @@
|
||||
# Nyanger is a simple logger designed to be simple to use and simple to modify.
|
||||
#
|
||||
# Copyright (C) 2024 Kirill Harmatulla Shakirov kirill.shakirov@protonmail.com
|
||||
#
|
||||
# This program is free software: you can redistribute it and/or modify
|
||||
# it under the terms of the GNU General Public License as published by
|
||||
# the Free Software Foundation, either version 3 of the License, or
|
||||
# (at your option) any later version.
|
||||
#
|
||||
# This program is distributed in the hope that it will be useful,
|
||||
# but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
# GNU General Public License for more details.
|
||||
#
|
||||
# You should have received a copy of the GNU General Public License
|
||||
# along with this program. If not, see <https://www.gnu.org/licenses/>.
|
||||
|
||||
import asyncio
|
||||
from nyanger.asynchronous.nyan import LogLevel, LogMessage, LogWriter
|
||||
|
||||
|
||||
class FileWriter(LogWriter):
|
||||
"""
|
||||
Simple implementation of LogWriter.
|
||||
Writes log messages to file
|
||||
"""
|
||||
def __init__(self, file_name: str, loging_level: LogLevel = LogLevel.DEBUG):
|
||||
"""
|
||||
Initialize FileWriter instance.
|
||||
:param file_name: log file name
|
||||
:param loging_level: messages with severity less than this field value will be filtered out.
|
||||
"""
|
||||
self._loging_level = loging_level
|
||||
self._file_name = file_name
|
||||
self._log_file = None
|
||||
|
||||
async def start(self, loop: asyncio.AbstractEventLoop):
|
||||
self._log_file = open(self._file_name, mode="ta")
|
||||
|
||||
async def write(self, msg: LogMessage):
|
||||
"""
|
||||
Formats and writes msg to log file.
|
||||
:param msg: message to be logged.
|
||||
"""
|
||||
if msg.severity.value <= self._loging_level.value:
|
||||
log_text = f"{msg.time.isoformat()} {msg.severity.name}: {msg.text}\n"
|
||||
await asyncio.to_thread(self._log_file.write, (log_text,))
|
||||
# await asyncio.to_thread(self._log_file.flush)
|
||||
|
||||
async def stop(self):
|
||||
self._log_file.flush()
|
||||
self._log_file.close()
|
||||
@@ -55,8 +55,8 @@ class LogWriter(ABC):
|
||||
Abstract class representing log writer interface.
|
||||
Must be implemented by a concrete log writer class.
|
||||
Only write method must be implemented, start and stop methods one should implement as needed.
|
||||
Start method will be called by Nyanger inside log precess on its start.
|
||||
And stop method will be called inside log process after exiting logging loop.
|
||||
Start method will be called by Nyanger instance on its start.
|
||||
And stop method will be called when stop() method of Nyanger instance called.
|
||||
"""
|
||||
@abstractmethod
|
||||
async def start(self, loop: asyncio.AbstractEventLoop):
|
||||
@@ -144,7 +144,7 @@ class Nyanger:
|
||||
Sending stop message to logging process and waits timeout seconds for process end.
|
||||
If process still active after timeout seconds - terminates it.
|
||||
:param timeout: Number of seconds to wait for process to terminate.
|
||||
:param supress_timeout_error: Pum
|
||||
:param supress_timeout_error: Supress timeout error raised when logger fails to stop in time.
|
||||
:return:
|
||||
"""
|
||||
await self._log_queue.put(self._STOP_MESSAGE)
|
||||
|
||||
@@ -0,0 +1,50 @@
|
||||
# Nyanger is a simple logger designed to be simple to use and simple to modify.
|
||||
#
|
||||
# Copyright (C) 2024 Kirill Harmatulla Shakirov kirill.shakirov@protonmail.com
|
||||
#
|
||||
# This program is free software: you can redistribute it and/or modify
|
||||
# it under the terms of the GNU General Public License as published by
|
||||
# the Free Software Foundation, either version 3 of the License, or
|
||||
# (at your option) any later version.
|
||||
#
|
||||
# This program is distributed in the hope that it will be useful,
|
||||
# but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
# GNU General Public License for more details.
|
||||
#
|
||||
# You should have received a copy of the GNU General Public License
|
||||
# along with this program. If not, see <https://www.gnu.org/licenses/>.
|
||||
|
||||
from nyanger.process.nyan import LogLevel, LogMessage, LogWriter
|
||||
|
||||
class FileWriter(LogWriter):
|
||||
"""
|
||||
Simple implementation of LogWriter.
|
||||
Writes log messages to file
|
||||
"""
|
||||
def __init__(self, file_name: str, loging_level: LogLevel = LogLevel.DEBUG):
|
||||
"""
|
||||
Initialize FileWriter instance.
|
||||
:param file_name: log file name
|
||||
:param loging_level: messages with severity less than this field value will be filtered out.
|
||||
"""
|
||||
self._loging_level = loging_level
|
||||
self._file_name = file_name
|
||||
self._log_file = None
|
||||
|
||||
def start(self):
|
||||
self._log_file = open(self._file_name, mode="ta")
|
||||
|
||||
def write(self, msg: LogMessage):
|
||||
"""
|
||||
Formats and writes msg to log file.
|
||||
:param msg: message to be logged.
|
||||
"""
|
||||
if msg.severity.value <= self._loging_level.value:
|
||||
log_text = f"{msg.time.isoformat()} {msg.severity.name}: {msg.text}\n"
|
||||
self._log_file.write(log_text)
|
||||
self._log_file.flush()
|
||||
|
||||
def stop(self):
|
||||
self._log_file.flush()
|
||||
self._log_file.close()
|
||||
@@ -0,0 +1,50 @@
|
||||
# Nyanger is a simple logger designed to be simple to use and simple to modify.
|
||||
#
|
||||
# Copyright (C) 2024 Kirill Harmatulla Shakirov kirill.shakirov@protonmail.com
|
||||
#
|
||||
# This program is free software: you can redistribute it and/or modify
|
||||
# it under the terms of the GNU General Public License as published by
|
||||
# the Free Software Foundation, either version 3 of the License, or
|
||||
# (at your option) any later version.
|
||||
#
|
||||
# This program is distributed in the hope that it will be useful,
|
||||
# but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
# GNU General Public License for more details.
|
||||
#
|
||||
# You should have received a copy of the GNU General Public License
|
||||
# along with this program. If not, see <https://www.gnu.org/licenses/>.
|
||||
|
||||
from nyanger.simple.nyan import LogLevel, LogMessage, LogWriter
|
||||
|
||||
class FileWriter(LogWriter):
|
||||
"""
|
||||
Simple implementation of LogWriter.
|
||||
Writes log messages to file
|
||||
"""
|
||||
def __init__(self, file_name: str, loging_level: LogLevel = LogLevel.DEBUG):
|
||||
"""
|
||||
Initialize FileWriter instance.
|
||||
:param file_name: log file name
|
||||
:param loging_level: messages with severity less than this field value will be filtered out.
|
||||
"""
|
||||
self._loging_level = loging_level
|
||||
self._file_name = file_name
|
||||
self._log_file = None
|
||||
|
||||
def start(self):
|
||||
self._log_file = open(self._file_name, mode="ta")
|
||||
|
||||
def write(self, msg: LogMessage):
|
||||
"""
|
||||
Formats and writes msg to log file.
|
||||
:param msg: message to be logged.
|
||||
"""
|
||||
if msg.severity.value <= self._loging_level.value:
|
||||
log_text = f"{msg.time.isoformat()} {msg.severity.name}: {msg.text}\n"
|
||||
self._log_file.write(log_text)
|
||||
self._log_file.flush()
|
||||
|
||||
def stop(self):
|
||||
self._log_file.flush()
|
||||
self._log_file.close()
|
||||
Reference in New Issue
Block a user