|
19 | 19 | import os |
20 | 20 | import sys |
21 | 21 |
|
| 22 | +def get_app_directory(probing_path): |
| 23 | + return os.path.dirname(os.path.abspath(os.path.dirname(probing_path))) |
22 | 24 |
|
23 | | -def configure(name, path=None): |
| 25 | +def configure(name, probing_path=None, app_root=None): |
24 | 26 | """ Configure logging and return a logger and the location of its logging |
25 | 27 | configuration file. |
26 | 28 |
|
@@ -57,63 +59,66 @@ def configure(name, path=None): |
57 | 59 | return. |
58 | 60 |
|
59 | 61 | You may short circuit the search for a logging configuration file by |
60 | | - providing an alternative file location in `path`. Logging configuration |
| 62 | + providing an alternative file location in `probing_path`. Logging configuration |
61 | 63 | files must be in `ConfigParser format`_. |
62 | 64 |
|
63 | 65 | #Arguments: |
64 | 66 |
|
65 | 67 | :param name: Logger name |
66 | 68 | :type name: str |
67 | | - :param path: Location of an alternative logging configuration file or `None` |
68 | | - :type path: str or NoneType |
| 69 | + :param probing_path: Location of an alternative logging configuration file or `None` |
| 70 | + :type probing_path: str or NoneType |
69 | 71 | :returns: A logger and the location of its logging configuration file |
| 72 | + :param app_root: The root of the application directory, used primarily by tests. |
| 73 | + :type app_root: str or NoneType |
70 | 74 |
|
71 | 75 | .. _ConfigParser format: http://goo.gl/K6edZ8 |
72 | 76 |
|
73 | 77 | """ |
74 | | - app_directory = os.path.dirname(os.path.dirname(os.path.realpath(sys.argv[0]))) |
75 | 78 |
|
76 | | - if path is None: |
77 | | - probing_path = [ |
| 79 | + app_directory = get_app_directory(sys.argv[0]) if app_root is None else app_root |
| 80 | + |
| 81 | + if probing_path is None: |
| 82 | + probing_paths = [ |
78 | 83 | 'local/%s.logging.conf' % name, |
79 | 84 | 'default/%s.logging.conf' % name, |
80 | 85 | 'local/logging.conf', |
81 | 86 | 'default/logging.conf'] |
82 | | - for relative_path in probing_path: |
| 87 | + for relative_path in probing_paths: |
83 | 88 | configuration_file = os.path.join(app_directory, relative_path) |
84 | 89 | if os.path.exists(configuration_file): |
85 | | - path = configuration_file |
| 90 | + probing_path = configuration_file |
86 | 91 | break |
87 | | - elif not os.path.isabs(path): |
| 92 | + elif not os.path.isabs(probing_path): |
88 | 93 | found = False |
89 | 94 | for conf in 'local', 'default': |
90 | | - configuration_file = os.path.join(app_directory, conf, path) |
| 95 | + configuration_file = os.path.join(app_directory, conf, probing_path) |
91 | 96 | if os.path.exists(configuration_file): |
92 | | - path = configuration_file |
| 97 | + probing_path = configuration_file |
93 | 98 | found = True |
94 | 99 | break |
95 | 100 | if not found: |
96 | 101 | raise ValueError( |
97 | 102 | 'Logging configuration file "%s" not found in local or default ' |
98 | | - 'directory' % path) |
99 | | - elif not os.path.exists(path): |
| 103 | + 'directory' % probing_path) |
| 104 | + elif not os.path.exists(probing_path): |
100 | 105 | raise ValueError('Logging configuration file "%s" not found') |
101 | 106 |
|
102 | | - if path is not None: |
| 107 | + if probing_path is not None: |
103 | 108 | working_directory = os.getcwd() |
104 | 109 | os.chdir(app_directory) |
105 | 110 | try: |
106 | 111 | splunk_home = os.path.normpath(os.path.join(working_directory, os.environ['SPLUNK_HOME'])) |
107 | 112 | except KeyError: |
108 | 113 | splunk_home = working_directory # reasonable in debug scenarios |
109 | 114 | try: |
110 | | - path = os.path.abspath(path) |
111 | | - fileConfig(path, {'SPLUNK_HOME': splunk_home}) |
| 115 | + probing_path = os.path.abspath(probing_path) |
| 116 | + fileConfig(probing_path, {'SPLUNK_HOME': splunk_home}) |
112 | 117 | finally: |
113 | 118 | os.chdir(working_directory) |
114 | 119 |
|
115 | 120 | if len(root.handlers) == 0: |
116 | 121 | root.addHandler(StreamHandler()) |
117 | 122 |
|
118 | 123 | logger = getLogger(name) |
119 | | - return logger, path |
| 124 | + return logger, probing_path |
0 commit comments