Skip to content

Commit

Permalink
add systemd log
Browse files Browse the repository at this point in the history
  • Loading branch information
honjow committed Apr 26, 2024
1 parent 06e865a commit f1a992c
Show file tree
Hide file tree
Showing 2 changed files with 38 additions and 7 deletions.
24 changes: 17 additions & 7 deletions backend/config.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,18 +2,28 @@
import logging
import glob
import yaml
import traceback
import decky_plugin
from helpers import get_homebrew_path
from logging_handler import SystemdHandler

# 日志配置
LOG_LOCATION = "/tmp/PowerControl_py.log"
logging.basicConfig(
level=logging.INFO,
filename=LOG_LOCATION,
format="[%(asctime)s | %(filename)s:%(lineno)s:%(funcName)s] %(levelname)s: %(message)s",
filemode="w",
force=True,
)
try:
logging.basicConfig(
level=logging.INFO,
format="[%(asctime)s | %(filename)s:%(lineno)s:%(funcName)s] %(levelname)s: %(message)s",
force=True,
handlers=[
SystemdHandler(),
logging.FileHandler(filename=LOG_LOCATION, mode="w"),
],
)
except Exception as e:
stack = traceback.format_exc()
with open(LOG_LOCATION, "a") as f:
f.write(str(e))
f.write(stack)

# 路径配置
try:
Expand Down
21 changes: 21 additions & 0 deletions backend/logging_handler.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
import logging
import subprocess

LOG = "/tmp/PowerControl_systemd.log"


class SystemdHandler(logging.Handler):
def emit(self, record):
msg = self.format(record)
try:
subprocess.run(["systemd-cat", "-t", "powercontrol"], input=msg, text=True)
except Exception as e:
self.write_log(f"systemd-cat error: {e}")

def write_log(self, msg):
try:
with open(LOG, "a") as f:
f.write(msg)
f.write("\n")
except Exception as e:
print(e)

0 comments on commit f1a992c

Please sign in to comment.