You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

123 lines
3.3 KiB

4 years ago
  1. # -*- coding: utf-8 -*-
  2. #
  3. # This file is part of Glances.
  4. #
  5. # Copyright (C) 2018 Nicolargo <nicolas@nicolargo.com>
  6. #
  7. # Glances is free software; you can redistribute it and/or modify
  8. # it under the terms of the GNU Lesser General Public License as published by
  9. # the Free Software Foundation, either version 3 of the License, or
  10. # (at your option) any later version.
  11. #
  12. # Glances is distributed in the hope that it will be useful,
  13. # but WITHOUT ANY WARRANTY; without even the implied warranty of
  14. # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  15. # GNU Lesser General Public License for more details.
  16. #
  17. # You should have received a copy of the GNU Lesser General Public License
  18. # along with this program. If not, see <http://www.gnu.org/licenses/>.
  19. """Custom logger class."""
  20. import os
  21. import json
  22. import getpass
  23. import tempfile
  24. import logging
  25. import logging.config
  26. LOG_FILENAME = os.path.join(tempfile.gettempdir(),
  27. 'glances-{}.log'.format(getpass.getuser()))
  28. # Define the logging configuration
  29. LOGGING_CFG = {
  30. "version": 1,
  31. "disable_existing_loggers": "False",
  32. "root": {
  33. "level": "INFO",
  34. "handlers": ["file", "console"]
  35. },
  36. "formatters": {
  37. "standard": {
  38. "format": "%(asctime)s -- %(levelname)s -- %(message)s"
  39. },
  40. "short": {
  41. "format": "%(levelname)s -- %(message)s"
  42. },
  43. "long": {
  44. "format": "%(asctime)s -- %(levelname)s -- %(message)s (%(funcName)s in %(filename)s)"
  45. },
  46. "free": {
  47. "format": "%(message)s"
  48. }
  49. },
  50. "handlers": {
  51. "file": {
  52. "level": "DEBUG",
  53. "class": "logging.handlers.RotatingFileHandler",
  54. "formatter": "standard",
  55. "filename": LOG_FILENAME
  56. },
  57. "console": {
  58. "level": "CRITICAL",
  59. "class": "logging.StreamHandler",
  60. "formatter": "free"
  61. }
  62. },
  63. "loggers": {
  64. "debug": {
  65. "handlers": ["file", "console"],
  66. "level": "DEBUG"
  67. },
  68. "verbose": {
  69. "handlers": ["file", "console"],
  70. "level": "INFO"
  71. },
  72. "standard": {
  73. "handlers": ["file"],
  74. "level": "INFO"
  75. },
  76. "requests": {
  77. "handlers": ["file", "console"],
  78. "level": "ERROR"
  79. },
  80. "elasticsearch": {
  81. "handlers": ["file", "console"],
  82. "level": "ERROR"
  83. },
  84. "elasticsearch.trace": {
  85. "handlers": ["file", "console"],
  86. "level": "ERROR"
  87. }
  88. }
  89. }
  90. def glances_logger(env_key='LOG_CFG'):
  91. """Build and return the logger.
  92. env_key define the env var where a path to a specific JSON logger
  93. could be defined
  94. :return: logger -- Logger instance
  95. """
  96. _logger = logging.getLogger()
  97. # By default, use the LOGGING_CFG logger configuration
  98. config = LOGGING_CFG
  99. # Check if a specific configuration is available
  100. user_file = os.getenv(env_key, None)
  101. if user_file and os.path.exists(user_file):
  102. # A user file as been defined. Use it...
  103. with open(user_file, 'rt') as f:
  104. config = json.load(f)
  105. # Load the configuration
  106. logging.config.dictConfig(config)
  107. return _logger
  108. logger = glances_logger()