feat: refine log system (#142)

* refine package logging

* add interface

* revert version selection

* replace tuple with model

* rename column in logs table, add coverters

* generate process identifier for child proocesses
This commit is contained in:
2025-03-09 14:46:33 +02:00
committed by GitHub
parent 08640d9108
commit 6f57ed550b
38 changed files with 711 additions and 209 deletions

View File

@@ -21,11 +21,10 @@ from aiohttp.web import HTTPBadRequest, HTTPNoContent, HTTPNotFound, Response, j
from ahriman.core.exceptions import UnknownPackageError
from ahriman.core.utils import pretty_datetime
from ahriman.models.log_record_id import LogRecordId
from ahriman.models.log_record import LogRecord
from ahriman.models.user_access import UserAccess
from ahriman.web.apispec.decorators import apidocs
from ahriman.web.schemas import LogsSchema, PackageNameSchema, PackageVersionSchema, RepositoryIdSchema, \
VersionedLogSchema
from ahriman.web.schemas import LogSchema, LogsSchema, PackageNameSchema, PackageVersionSchema, RepositoryIdSchema
from ahriman.web.views.base import BaseView
from ahriman.web.views.status_view_guard import StatusViewGuard
@@ -97,7 +96,7 @@ class LogsView(StatusViewGuard, BaseView):
response = {
"package_base": package_base,
"status": status.view(),
"logs": "\n".join(f"[{pretty_datetime(created)}] {message}" for created, message in logs)
"logs": "\n".join(f"[{pretty_datetime(log_record.created)}] {log_record.message}" for log_record in logs)
}
return json_response(response)
@@ -109,7 +108,7 @@ class LogsView(StatusViewGuard, BaseView):
error_400_enabled=True,
error_404_description="Repository is unknown",
match_schema=PackageNameSchema,
body_schema=VersionedLogSchema,
body_schema=LogSchema,
)
async def post(self) -> None:
"""
@@ -123,12 +122,10 @@ class LogsView(StatusViewGuard, BaseView):
try:
data = await self.request.json()
created = data["created"]
record = data["message"]
version = data["version"]
log_record = LogRecord.from_json(package_base, data)
except Exception as ex:
raise HTTPBadRequest(reason=str(ex))
self.service().package_logs_add(LogRecordId(package_base, version), created, record)
self.service().package_logs_add(log_record)
raise HTTPNoContent

View File

@@ -0,0 +1,63 @@
#
# Copyright (c) 2021-2025 ahriman team.
#
# This file is part of ahriman
# (see https://github.com/arcan1s/ahriman).
#
# 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 <http://www.gnu.org/licenses/>.
#
from aiohttp.web import HTTPBadRequest, HTTPNoContent
from ahriman.models.user_access import UserAccess
from ahriman.web.apispec.decorators import apidocs
from ahriman.web.schemas import LogsRotateSchema
from ahriman.web.views.base import BaseView
class LogsView(BaseView):
"""
logs management web view
Attributes:
DELETE_PERMISSION(UserAccess): (class attribute) delete permissions of self
"""
DELETE_PERMISSION = UserAccess.Full
ROUTES = ["/api/v1/service/logs"]
@apidocs(
tags=["Actions"],
summary="Rotate logs",
description="Remove older logs from system",
permission=DELETE_PERMISSION,
error_400_enabled=True,
error_404_description="Repository is unknown",
query_schema=LogsRotateSchema,
)
async def delete(self) -> None:
"""
rotate logs from system
Raises:
HTTPBadRequest: if bad data is supplied
HTTPNoContent: on success response
"""
try:
keep_last_records = int(self.request.query.get("keep_last_records", 0))
except Exception as ex:
raise HTTPBadRequest(reason=str(ex))
self.service().logs_rotate(keep_last_records)
raise HTTPNoContent

View File

@@ -63,10 +63,5 @@ class LogsView(StatusViewGuard, BaseView):
logs = self.service(package_base=package_base).package_logs_get(package_base, limit, offset)
response = [
{
"created": created,
"message": message,
} for created, message in logs
]
response = [log_record.view() for log_record in logs]
return json_response(response)