• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

openmrs / openmrs-core / 26982556974

04 Jun 2026 10:08PM UTC coverage: 63.677% (+0.3%) from 63.427%
26982556974

push

github

ibacher
TRUNK-6392: Improvements to the OpenMRS Logging plugin (#6104)

151 of 195 new or added lines in 8 files covered. (77.44%)

12 existing lines in 5 files now uncovered.

23830 of 37423 relevant lines covered (63.68%)

0.64 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

88.46
/api/src/main/java/org/openmrs/logging/OpenmrsPropertyLookup.java
1
/**
2
 * This Source Code Form is subject to the terms of the Mozilla Public License,
3
 * v. 2.0. If a copy of the MPL was not distributed with this file, You can
4
 * obtain one at http://mozilla.org/MPL/2.0/. OpenMRS is also distributed under
5
 * the terms of the Healthcare Disclaimer located at http://openmrs.org/license.
6
 *
7
 * Copyright (C) OpenMRS Inc. OpenMRS is a registered trademark and the OpenMRS
8
 * graphic logo is a trademark of OpenMRS Inc.
9
 */
10
package org.openmrs.logging;
11

12
import org.apache.logging.log4j.core.LogEvent;
13
import org.apache.logging.log4j.core.config.plugins.Plugin;
14
import org.apache.logging.log4j.core.lookup.AbstractLookup;
15
import org.apache.logging.log4j.core.lookup.StrLookup;
16
import org.apache.logging.log4j.status.StatusLogger;
17
import org.openmrs.api.ServiceNotFoundException;
18
import org.openmrs.api.context.Context;
19
import org.openmrs.util.ConfigUtil;
20
import org.openmrs.util.OpenmrsConstants;
21
import org.openmrs.util.OpenmrsUtil;
22
import org.openmrs.util.PrivilegeConstants;
23

24
/**
25
 * This class exposes a subset of OpenMRS properties to the log4j context configuration. This is intended to allow the
26
 * logger to make use of certain OpenMRS properties.
27
 * <p/>
28
 * To use these properties in your logger configuration, reference them like <tt>${openmrs:&lt;property&gt;}</tt>, e.g.
29
 * <tt>${openmrs:applicationDirectory}</tt>.
30
 * <p/>
31
 * Supported properties:
32
 * <dl>
33
 *     <dt>applicationDirectory</dt>
34
 *     <dd>The OpenMRS application directory as a string</dd>
35
 *     <dt>logLocation</dt>
36
 *     <dd>The current value for the <tt>log.location</tt> setting</dd>
37
 *     <dt>logLayout</dt>
38
 *     <dd>The current value for the <tt>log.layout</tt> setting</dd>
39
 * </dl>
40
 * <p/>
41
 * Care should be taken in exposing information through this class to ensure that no secrets are
42
 * leaked or properties that expose potentially unsafe operations based on user input.
43
 */
44
@Plugin(name = OpenmrsPropertyLookup.NAME, category = StrLookup.CATEGORY)
45
@SuppressWarnings("unused")
46
public class OpenmrsPropertyLookup extends AbstractLookup {
1✔
47
        
48
        public static final String NAME = "openmrs";
49
        
50
        @Override
51
        public String lookup(LogEvent event, String key) {
52
                switch (key) {
1✔
53
                        case "applicationDirectory":
54
                                final String applicationDirectory = OpenmrsUtil.getApplicationDataDirectory();
1✔
55
                                return applicationDirectory == null || applicationDirectory.isEmpty() ? null : applicationDirectory;
1✔
56
                        case "logLocation":
57
                                final String logLocation = getProperty(OpenmrsConstants.GP_LOG_LOCATION);
1✔
58
                                return logLocation == null ? null
1✔
59
                                        : logLocation.endsWith("/") ? logLocation.substring(0, logLocation.length() - 1) : logLocation;
1✔
60
                        case "logLayout":
61
                                return getProperty(OpenmrsConstants.GP_LOG_LAYOUT);
1✔
62
                        default:
63
                                StatusLogger.getLogger().error(
1✔
64
                                    "{} is not a supported property. We support openmrs:applicationDirectory, openmrs:logLocation, and openmrs:logLayout",
65
                                    key);
66
                                return null;
1✔
67
                }
68
        }
69

70
        private String getProperty(String propertyName) {
71
                String value = ConfigUtil.getSystemProperty(propertyName);
1✔
72
                if (value == null) {
1✔
73
                        value = ConfigUtil.getRuntimeProperty(propertyName);
1✔
74
                }
75

76
                if (value == null && Context.isSessionOpen()) {
1✔
77
                        Context.addProxyPrivilege(PrivilegeConstants.GET_GLOBAL_PROPERTIES);
1✔
78
                        try {
79
                                value = ConfigUtil.getGlobalProperty(propertyName);
1✔
NEW
80
                        } catch (ServiceNotFoundException e) {
×
NEW
81
                                StatusLogger.getLogger().error("An exception was thrown while trying to get the \"{}\" global property",
×
82
                                    propertyName, e);
NEW
83
                                return null;
×
84
                        } finally {
85
                                Context.removeProxyPrivilege(PrivilegeConstants.GET_GLOBAL_PROPERTIES);
1✔
86
                        }
87
                }
88

89
                if (value == null || value.trim().isEmpty()) {
1✔
90
                        return getPropertyDefault(propertyName);
1✔
91
                }
92

93
                return value.trim();
1✔
94
        }
95

96
        private static String getPropertyDefault(String propertyName) {
97
                if (OpenmrsConstants.GP_LOG_LAYOUT.equals(propertyName)) {
1✔
98
                        return OpenmrsConstants.DEFAULT_LOG_LAYOUT_PATTERN;
1✔
99
                }
100
                return null;
1✔
101
        }
102
}
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2026 Coveralls, Inc