[virt-who/systemd: 2/2] Use journal for logging when available
by Radek Novacek
commit 865efb828eb68d88b8bf16aa6b267298abc587a5
Author: Radek Novacek <rnovacek(a)redhat.com>
Date: Tue Nov 27 12:35:33 2012 +0100
Use journal for logging when available
log.py | 51 ++++++++++++++++++++++++++++++++++++++++++++++++---
1 files changed, 48 insertions(+), 3 deletions(-)
---
diff --git a/log.py b/log.py
index e415001..bc85544 100644
--- a/log.py
+++ b/log.py
@@ -24,6 +24,40 @@ import logging.handlers
import os
import sys
+journalEnabled = False
+try:
+ from systemd import journal
+ journalEnabled = True
+except ImportError:
+ pass
+
+class NoExceptionFormatter(logging.Formatter):
+ def format(self, record):
+ if record.exc_info is not None:
+ record.exc_text = "\t" + str(record.exc_info[1])
+ return logging.Formatter.format(self, record)
+
+class JournalHandler(logging.Handler):
+ def __init__(self, level=logging.NOTSET):
+ logging.Handler.__init__(self, level)
+
+ def emit(self, record):
+ if journalEnabled:
+ try:
+ msg = self.format(record)
+ args = ['MESSAGE=' + record.message,
+ 'LOGGER=' + record.name,
+ 'THREAD_NAME=' + record.threadName,
+ 'CODE_FILE=' + record.pathname,
+ 'CODE_LINE=%d' % record.lineno,
+ 'CODE_FUNC=' + record.funcName]
+
+ journal.sendv(*args)
+ except (KeyboardInterrupt, SystemExit):
+ raise
+ except:
+ self.handleError(record)
+
def getLogger(debug, background):
logger = logging.getLogger("rhsm-app")
logger.setLevel(logging.DEBUG)
@@ -56,10 +90,21 @@ def getLogger(debug, background):
streamHandler.setLevel(logging.WARNING)
# Don't print exceptions to stdout in non-debug mode
- f = logging.Filter()
- f.filter = lambda record: record.exc_info is None
- streamHandler.addFilter(f)
+ streamHandler.setFormatter(NoExceptionFormatter())
+
logger.addHandler(streamHandler)
+ if os.getppid() == 1:
+ # Also log to journal if available
+ journalHandler = JournalHandler()
+ if debug:
+ journalHandler.setLevel(logging.DEBUG)
+ else:
+ journalHandler.setLevel(logging.WARNING)
+
+ # Don't print exceptions to journal in non-debug mode
+ journalHandler.setFormatter(NoExceptionFormatter())
+
+ logger.addHandler(journalHandler)
return logger
11 years, 5 months
[virt-who/systemd: 1/2] Add .service file for systemd
by Radek Novacek
commit 6ee26970e80b2b9705e970ec3547ca3c97e8e52f
Author: Radek Novacek <rnovacek(a)redhat.com>
Date: Tue Nov 27 09:40:50 2012 +0100
Add .service file for systemd
Makefile | 4 ++--
virt-who.service | 13 +++++++++++++
2 files changed, 15 insertions(+), 2 deletions(-)
---
diff --git a/Makefile b/Makefile
index 6dee540..f7d066a 100644
--- a/Makefile
+++ b/Makefile
@@ -13,10 +13,10 @@ check:
pyflakes *.py
install:
- install -d $(DESTDIR)/usr/share/$(name)/ $(DESTDIR)/usr/bin $(DESTDIR)/etc/rc.d/init.d $(DESTDIR)/etc/sysconfig $(DESTDIR)/usr/share/man/man8/
+ install -d $(DESTDIR)/usr/share/$(name)/ $(DESTDIR)/usr/bin $(DESTDIR)/usr/lib/systemd/system $(DESTDIR)/etc/sysconfig $(DESTDIR)/usr/share/man/man8/
install -pm 0644 *.py $(DESTDIR)/usr/share/$(name)/
install virt-who $(DESTDIR)/usr/bin/
- install virt-who-initscript $(DESTDIR)/etc/rc.d/init.d/virt-who
+ install -pm 0644 virt-who.service $(DESTDIR)/usr/lib/systemd/system/
install -pm 0644 virt-who.conf $(DESTDIR)/etc/sysconfig/virt-who
gzip -c virt-who.8 > virt-who.8.gz
install -pm 0644 virt-who.8.gz $(DESTDIR)/usr/share/man/man8/
diff --git a/virt-who.service b/virt-who.service
new file mode 100644
index 0000000..74982c5
--- /dev/null
+++ b/virt-who.service
@@ -0,0 +1,13 @@
+[Unit]
+Description=Daemon for reporting virtual guest IDs to subscription-manager
+After=syslog.target libvirtd.service network.target
+
+[Service]
+Type=forking
+PIDFile=/var/run/virt-who.pid
+ExecStart=/usr/bin/virt-who
+EnvironmentFile=-/etc/sysconfig/virt-who
+
+[Install]
+WantedBy=multi-user.target
+
11 years, 5 months
[virt-who] Fix permissions for man page
by Radek Novacek
commit dcf804972d8a8480668c80fae7fc9d4a0fdcce92
Author: Radek Novacek <rnovacek(a)redhat.com>
Date: Tue Nov 27 13:01:13 2012 +0100
Fix permissions for man page
Makefile | 2 +-
1 files changed, 1 insertions(+), 1 deletions(-)
---
diff --git a/Makefile b/Makefile
index 1400e23..6dee540 100644
--- a/Makefile
+++ b/Makefile
@@ -19,7 +19,7 @@ install:
install virt-who-initscript $(DESTDIR)/etc/rc.d/init.d/virt-who
install -pm 0644 virt-who.conf $(DESTDIR)/etc/sysconfig/virt-who
gzip -c virt-who.8 > virt-who.8.gz
- install virt-who.8.gz $(DESTDIR)/usr/share/man/man8/
+ install -pm 0644 virt-who.8.gz $(DESTDIR)/usr/share/man/man8/
srpm: pack
rpmbuild --define "_sourcedir $(PWD)" --define "_specdir $(PWD)" --define "_srcrpmdir $(PWD)" -bs $(name).spec
11 years, 5 months
[virt-who] Fix version in Makefile
by Radek Novacek
commit 46ec9426fb0d560f66b5b1d31f147d744e54e016
Author: Radek Novacek <rnovacek(a)redhat.com>
Date: Tue Nov 27 12:55:59 2012 +0100
Fix version in Makefile
Makefile | 2 +-
1 files changed, 1 insertions(+), 1 deletions(-)
---
diff --git a/Makefile b/Makefile
index 0d0d861..1400e23 100644
--- a/Makefile
+++ b/Makefile
@@ -1,6 +1,6 @@
name = virt-who
-version = 0.7
+version = 0.8
.PHONY: pack check install srpm rpm rpmlint upload
11 years, 5 months