-
Notifications
You must be signed in to change notification settings - Fork 0
Read logging configuration file from snippets #301
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: feat/V2.0-next
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,20 +1,32 @@ | ||
| package utils; | ||
|
|
||
| import com.sinch.sdk.core.utils.StringUtil; | ||
| import java.io.IOException; | ||
| import java.io.InputStream; | ||
| import java.util.Optional; | ||
| import java.util.Properties; | ||
| import java.util.logging.LogManager; | ||
| import java.util.logging.Logger; | ||
|
|
||
| public class Settings { | ||
|
|
||
| private static final Logger LOGGER = Logger.getLogger(Settings.class.getName()); | ||
|
|
||
| private static final String CONFIG_FILE = "config.properties"; | ||
| private static final String LOGGING_CONFIG_FILE = "logging.properties"; | ||
|
|
||
| private static final Properties properties; | ||
|
|
||
| static { | ||
| initializeLogger(); | ||
| properties = new Properties(); | ||
| try { | ||
| // load a properties file from class path, inside static method | ||
| properties.load(Settings.class.getClassLoader().getResourceAsStream("config.properties")); | ||
| properties.load(Settings.class.getClassLoader().getResourceAsStream(CONFIG_FILE)); | ||
| } catch (Exception ioe) { | ||
| // ignore exception: properties file is just an helper | ||
| LOGGER.config( | ||
| String.format( | ||
| "Error reading '%s' configuration file: %s", CONFIG_FILE, ioe.getMessage())); | ||
| } | ||
| } | ||
|
|
||
|
|
@@ -70,4 +82,17 @@ public static Optional<String> getConversationRegion() { | |
| public static Optional<String> getPhoneNumber() { | ||
| return get("SINCH_PHONE_NUMBER"); | ||
| } | ||
|
|
||
| public static void initializeLogger() { | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This can be made private |
||
| try (InputStream logConfigInputStream = | ||
| Settings.class.getClassLoader().getResourceAsStream(LOGGING_CONFIG_FILE)) { | ||
| if (logConfigInputStream != null) { | ||
| LogManager.getLogManager().readConfiguration(logConfigInputStream); | ||
| } | ||
| } catch (IOException ioe) { | ||
| LOGGER.config( | ||
| String.format( | ||
| "Error reading '%s' configuration file: %s", LOGGING_CONFIG_FILE, ioe.getMessage())); | ||
|
Comment on lines
+93
to
+95
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. You are using the logger to log an error about the logger that may have not been initialized. Is it ok to do that, or the error message will be invisible? |
||
| } | ||
| } | ||
| } | ||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Nit: instead of throwing a NPE when the file is not found, you may check if the inputStream is not null before loading the properties and log that the config file is not found if null