]>
Commit | Line | Data |
---|---|---|
d2e6a577 FG |
1 | import logging |
2 | import os | |
3 | from ceph_volume import terminal | |
4 | from ceph_volume import conf | |
5 | ||
6 | BASE_FORMAT = "[%(name)s][%(levelname)-6s] %(message)s" | |
7 | FILE_FORMAT = "[%(asctime)s]" + BASE_FORMAT | |
8 | ||
9 | ||
f91f0fd5 | 10 | def setup(name='ceph-volume.log', log_path=None, log_level=None): |
d2e6a577 FG |
11 | log_path = log_path or conf.log_path |
12 | # if a non-root user calls help or other no-sudo-required command the | |
13 | # logger will fail to write to /var/lib/ceph/ so this /tmp/ path is used as | |
14 | # a fallback | |
15 | tmp_log_file = os.path.join('/tmp/', name) | |
16 | root_logger = logging.getLogger() | |
17 | # The default path is where all ceph log files are, and will get rotated by | |
18 | # Ceph's logrotate rules. | |
f91f0fd5 TL |
19 | log_level = log_level or "DEBUG" |
20 | log_level = getattr(logging, log_level.upper()) | |
21 | root_logger.setLevel(log_level) | |
d2e6a577 FG |
22 | |
23 | try: | |
24 | fh = logging.FileHandler(log_path) | |
25 | except (OSError, IOError) as err: | |
26 | terminal.warning("Falling back to /tmp/ for logging. Can't use %s" % log_path) | |
27 | terminal.warning(str(err)) | |
28 | conf.log_path = tmp_log_file | |
29 | fh = logging.FileHandler(tmp_log_file) | |
30 | ||
f91f0fd5 | 31 | fh.setLevel(log_level) |
d2e6a577 FG |
32 | fh.setFormatter(logging.Formatter(FILE_FORMAT)) |
33 | ||
34 | root_logger.addHandler(fh) | |
494da23a TL |
35 | |
36 | ||
37 | def setup_console(): | |
38 | # TODO: At some point ceph-volume should stop using the custom logger | |
39 | # interface that exists in terminal.py and use the logging module to | |
40 | # produce output for the terminal | |
41 | # Console Logger | |
42 | sh = logging.StreamHandler() | |
43 | sh.setFormatter(logging.Formatter('[terminal] %(message)s')) | |
44 | sh.setLevel(logging.DEBUG) | |
45 | ||
46 | terminal_logger = logging.getLogger('terminal') | |
47 | ||
48 | # allow all levels at root_logger, handlers control individual levels | |
49 | terminal_logger.addHandler(sh) |