|
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,64 +59,67 @@ 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 | | - # TODO: verify that reverting to this, instead of using sys.argv[0] works |
75 | | - app_directory = os.path.dirname(os.getcwd()) |
| 78 | + if app_root: |
| 79 | + print "app_root is " + app_root |
| 80 | + app_directory = get_app_directory(sys.argv[0]) if app_root is None else app_root |
76 | 81 |
|
77 | | - if path is None: |
78 | | - probing_path = [ |
| 82 | + if probing_path is None: |
| 83 | + probing_paths = [ |
79 | 84 | 'local/%s.logging.conf' % name, |
80 | 85 | 'default/%s.logging.conf' % name, |
81 | 86 | 'local/logging.conf', |
82 | 87 | 'default/logging.conf'] |
83 | | - for relative_path in probing_path: |
| 88 | + for relative_path in probing_paths: |
84 | 89 | configuration_file = os.path.join(app_directory, relative_path) |
85 | 90 | if os.path.exists(configuration_file): |
86 | | - path = configuration_file |
| 91 | + probing_path = configuration_file |
87 | 92 | break |
88 | | - elif not os.path.isabs(path): |
| 93 | + elif not os.path.isabs(probing_path): |
89 | 94 | found = False |
90 | 95 | for conf in 'local', 'default': |
91 | | - configuration_file = os.path.join(app_directory, conf, path) |
| 96 | + configuration_file = os.path.join(app_directory, conf, probing_path) |
92 | 97 | if os.path.exists(configuration_file): |
93 | | - path = configuration_file |
| 98 | + probing_path = configuration_file |
94 | 99 | found = True |
95 | 100 | break |
96 | 101 | if not found: |
97 | 102 | raise ValueError( |
98 | 103 | 'Logging configuration file "%s" not found in local or default ' |
99 | | - 'directory' % path) |
100 | | - elif not os.path.exists(path): |
| 104 | + 'directory' % probing_path) |
| 105 | + elif not os.path.exists(probing_path): |
101 | 106 | raise ValueError('Logging configuration file "%s" not found') |
102 | 107 |
|
103 | | - if path is not None: |
| 108 | + if probing_path is not None: |
104 | 109 | working_directory = os.getcwd() |
105 | 110 | os.chdir(app_directory) |
106 | 111 | try: |
107 | 112 | splunk_home = os.path.normpath(os.path.join(working_directory, os.environ['SPLUNK_HOME'])) |
108 | 113 | except KeyError: |
109 | 114 | splunk_home = working_directory # reasonable in debug scenarios |
110 | 115 | try: |
111 | | - path = os.path.abspath(path) |
112 | | - fileConfig(path, {'SPLUNK_HOME': splunk_home}) |
| 116 | + probing_path = os.path.abspath(probing_path) |
| 117 | + fileConfig(probing_path, {'SPLUNK_HOME': splunk_home}) |
113 | 118 | finally: |
114 | 119 | os.chdir(working_directory) |
115 | 120 |
|
116 | 121 | if len(root.handlers) == 0: |
117 | 122 | root.addHandler(StreamHandler()) |
118 | 123 |
|
119 | 124 | logger = getLogger(name) |
120 | | - return logger, path |
| 125 | + return logger, probing_path |
0 commit comments