This repository has been archived on 2023-02-21. You can view files and clone it, but cannot push or open issues or pull requests.
logtracker/logtracker/journal/management/commands/dump.py

35 lines
1.0 KiB
Python

from django.core.management.base import BaseCommand, CommandError
from logtracker.journal.models import Entry
import os
import textwrap
def bold(txt):
bold = "\033[1m"
end = "\033[0m"
return bold + txt + end
class Command(BaseCommand):
help = "Print entries"
def add_arguments(self, parser):
parser.add_argument("--full", action="store_true")
parser.add_argument("-n", "--lines", default=100)
def handle(self, *args, **options):
_, columns = os.popen("stty size", "r").read().split()
for entry in Entry.objects.dump(lines=options['lines']):
line = "%s %s %s %s" % (
entry.timestamp.astimezone().strftime("%b %d %X"),
entry.host,
entry.unit,
entry.data.get("MESSAGE"),
)
priority = entry.data.get("PRIORITY")
if not options.get("full"):
line = textwrap.shorten(line, int(columns))
if priority and int(priority) < 4:
line = bold(line)
print(line)