Register
Login
Resources
Docs Blog Datasets Glossary Case Studies Tutorials & Webinars
Product
Data Engine LLMs Platform Enterprise
Pricing Explore
Connect to our Discord channel

#869 Add DagsHub Logger to Super Gradients

Merged
Ghost merged 1 commits into Deci-AI:master from timho102003:dagshub_logger
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
  1. import logging
  2. import os
  3. import sys
  4. import time
  5. from typing import Union
  6. from super_gradients.common.environment.env_variables import env_variables
  7. class AutoLoggerConfig:
  8. """
  9. A Class for the Automated Logging Config
  10. """
  11. filename: Union[str, None]
  12. def __init__(self):
  13. self.filename = None
  14. def _setup_default_logging(self, log_level: str = None) -> None:
  15. """
  16. Setup default logging configuration. Usually happens when app starts, and we don't have
  17. experiment dir yet.
  18. The default log directory will be `~/sg_logs`
  19. :param log_level: The default log level to use. If None, uses LOG_LEVEL and CONSOLE_LOG_LEVEL environment vars.
  20. :return: None
  21. """
  22. # There is no _easy_ way to log all events to a single file, when using DDP or DataLoader with num_workers > 1
  23. # on Windows platform. In both these cases a multiple processes will be spawned and multiple logs may be created.
  24. # Therefore the log file will have the parent PID to being able to discriminate the logs corresponding to a single run.
  25. timestamp = time.strftime("%Y_%m_%d_%H_%M_%S", time.localtime())
  26. self._setup_logging(
  27. filename=os.path.expanduser(f"~/sg_logs/logs_{os.getppid()}_{timestamp}.log"),
  28. copy_already_logged_messages=False,
  29. filemode="w",
  30. log_level=log_level,
  31. )
  32. def _setup_logging(self, filename: str, copy_already_logged_messages: bool, filemode: str = "a", log_level: str = None) -> None:
  33. """
  34. Sets the logging configuration to store messages to specific file
  35. :param filename: Output log file
  36. :param filemode: Open mode for file
  37. :param copy_already_logged_messages: Controls whether messages from previous log configuration should be copied
  38. to new place. This is helpful to transfer diagnostic messages (from the app start) to experiment dir.
  39. :param log_level: The default log level to use. If None, uses LOG_LEVEL and CONSOLE_LOG_LEVEL environment vars.
  40. :return:
  41. """
  42. os.makedirs(os.path.dirname(filename), exist_ok=True)
  43. if copy_already_logged_messages and self.filename is not None and os.path.exists(self.filename):
  44. with open(self.filename, "r", encoding="utf-8") as src:
  45. with open(filename, "w") as dst:
  46. dst.write(src.read())
  47. file_logging_level = log_level or env_variables.FILE_LOG_LEVEL
  48. console_logging_level = log_level or env_variables.CONSOLE_LOG_LEVEL
  49. cur_version = sys.version_info
  50. python_38 = (3, 8)
  51. python_39 = (3, 9)
  52. manager = logging.getLogger("").manager
  53. extra_kwargs = {}
  54. if cur_version >= python_38:
  55. extra_kwargs = dict(
  56. force=True,
  57. )
  58. else:
  59. # If the logging does not support force=True, we should manually delete handlers
  60. del manager.root.handlers[:]
  61. if cur_version >= python_39:
  62. extra_kwargs["encoding"] = "utf-8"
  63. logging.basicConfig(
  64. filename=filename,
  65. filemode=filemode,
  66. format="%(asctime)s %(levelname)s - %(name)s - %(message)s",
  67. datefmt="[%Y-%m-%d %H:%M:%S]",
  68. level=file_logging_level,
  69. **extra_kwargs,
  70. )
  71. # Add console handler
  72. console_handler = logging.StreamHandler()
  73. console_handler.setLevel(console_logging_level)
  74. console_handler.setFormatter(
  75. logging.Formatter(
  76. "%(asctime)s %(levelname)s - %(filename)s - %(message)s",
  77. datefmt="[%Y-%m-%d %H:%M:%S]",
  78. )
  79. )
  80. manager.root.handlers.append(console_handler)
  81. self.filename = filename
  82. @classmethod
  83. def get_instance(cls):
  84. global _super_gradients_logger_config
  85. if _super_gradients_logger_config is None:
  86. _super_gradients_logger_config = cls()
  87. _super_gradients_logger_config._setup_default_logging()
  88. return _super_gradients_logger_config
  89. @classmethod
  90. def get_log_file_path(cls) -> str:
  91. """
  92. Return the current log file used to store log messages
  93. :return: Full path to log file
  94. """
  95. self = cls.get_instance()
  96. return self.filename
  97. @classmethod
  98. def setup_logging(cls, filename: str, copy_already_logged_messages: bool, filemode: str = "a", log_level: str = None) -> None:
  99. self = cls.get_instance()
  100. self._setup_logging(filename, copy_already_logged_messages, filemode, log_level)
  101. _super_gradients_logger_config = None
Discard
Tip!

Press p or to see the previous file or, n or to see the next file