Add the username to the log filename

Glances as system tool is often executed in a multi-user environment or
even with root privileges. Then, make the log file system-aware as well.
This commit is contained in:
Alessio Sergi 2016-12-27 19:49:37 +01:00
parent 555f84b976
commit 3baae1f5c4
5 changed files with 11 additions and 24 deletions

2
NEWS
View File

@ -9,6 +9,8 @@ Changes:
* The curses interface on Windows is no more. The web-based interface is now
the default. (issue #946)
* The name of the log file now contains the name of the current user logged in,
i.e., 'glances-USERNAME.log'.
* IRQ plugin off by default. '--disable-irq' option replaced by '--enable-irq'.
Enhancements and new features:

View File

@ -16,7 +16,7 @@ Command-Line Options
.. option:: -d, --debug
enable debug mode. The debugging output is saved to /tmp/glances.log.
enable debug mode. The debugging output is saved to /tmp/glances-USERNAME.log.
.. option:: -C CONF_FILE, --config CONF_FILE

View File

@ -101,16 +101,13 @@ Glances logs all of its internal messages to a log file.
``DEBUG`` messages can been logged using the ``-d`` option on the command
line.
By default, the ``glances.log`` file is under the temporary directory:
By default, the ``glances-USERNAME.log`` file is under the temporary directory:
===================== ==================================================
``Linux, *BSD, OS X`` /tmp
``Windows`` %APPDATA%\\Local\\temp
===================== ==================================================
If ``glances.log`` is not writable, a new file will be created and
returned to the user console.
If you want to use another system path or change the log message, you can use
your own logger configuration. First of all you have to create a glances.json
file with (for example) the following content (JSON format):

View File

@ -26,7 +26,7 @@ import threading
from glances.compat import Fault, ProtocolError, ServerProxy
from glances.autodiscover import GlancesAutoDiscoverServer
from glances.client import GlancesClient, GlancesClientTransport
from glances.logger import logger
from glances.logger import logger, LOG_FILENAME
from glances.password_list import GlancesPasswordList as GlancesPassword
from glances.static_list import GlancesStaticServer
from glances.outputs.glances_curses_browser import GlancesCursesBrowser
@ -189,7 +189,7 @@ class GlancesClientBrowser(object):
if not client.login():
self.screen.display_popup(
"Sorry, cannot connect to '{}'\n"
"See 'glances.log' for more details".format(server['name']))
"See '{}' for more details".format(server['name'], LOG_FILENAME))
# Set the ONLINE status for the selected server
self.set_in_selected('status', 'OFFLINE')

View File

@ -20,13 +20,14 @@
"""Custom logger class."""
import os
import tempfile
import json
import getpass
import tempfile
import logging
import logging.config
LOG_FILENAME = os.path.join(tempfile.gettempdir(), 'glances.log')
LOG_FILENAME = os.path.join(tempfile.gettempdir(), 'glances-{}.log'.format(getpass.getuser()))
# Define the logging configuration
LOGGING_CFG = {
@ -51,7 +52,8 @@ LOGGING_CFG = {
"file": {
"level": "DEBUG",
"class": "logging.handlers.RotatingFileHandler",
"formatter": "standard"
"formatter": "standard",
"filename": LOG_FILENAME
},
"console": {
"level": "CRITICAL",
@ -88,17 +90,6 @@ LOGGING_CFG = {
}
def tempfile_name():
"""Return the tempfile name (full path)."""
if os.access(LOG_FILENAME, os.F_OK) and not os.access(LOG_FILENAME, os.W_OK):
print("WARNING: Couldn't write to '{}' (Permission denied)".format(LOG_FILENAME))
ret = tempfile.mkstemp(prefix='glances-', suffix='.log', text=True)
print("Create log file at '{}'".format(ret[1]))
return ret[1]
return LOG_FILENAME
def glances_logger(env_key='LOG_CFG'):
"""Build and return the logger.
@ -109,9 +100,6 @@ def glances_logger(env_key='LOG_CFG'):
"""
_logger = logging.getLogger()
# Overwrite the default logger file
LOGGING_CFG['handlers']['file']['filename'] = tempfile_name()
# By default, use the LOGGING_CFG logger configuration
config = LOGGING_CFG