Skip to content

Commit

Permalink
JAMES-3221 #comment Improved logging around configuration files for d…
Browse files Browse the repository at this point in the history
…evops

* When loading a configuration file, print it's absolute
* Usefull for debugging production miss-configurations
* Added documentation
  • Loading branch information
ieugen committed Jun 17, 2020
1 parent 213d8d1 commit b90d05e
Show file tree
Hide file tree
Showing 12 changed files with 80 additions and 2 deletions.
43 changes: 43 additions & 0 deletions docs/modules/development/pages/logging.adoc
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
= Logging in Apache James

NOTE: This information targets developers.

Logging is very important when running a software.
Most of the time people use logs for debugging and troubleshooting issues in production.
In Apache James we leverage a logging facade for logging http://www.slf4j.org/[Slf4j] .
Please see the documentation for exact details.

In a typical application logs are stored for a specific period.
This can be 1 day, 1 week, etc.

Some other typical scenarios include:

* Rotating logs periodically.
* Shipping logs centrally.
* Saving some logging messages to other files / end devices
By leveraging a logging facade like Slf4j, the end user has the ability to use different logging back ends and achieve the above requirements.

Logging is something we do to help the people running our code.
We also help ourselves when they encounter bugs, and they need to share information with us for a fix.
As developers, we should be mindful of the logging statements.
We should also be mindful about the performance implications of logging and not abuse it.

== Loggers used in code

You can define a logger using a string or a class.
For common loggers it's ok to use a string name.

[source,java]
--
private static final Logger LOGGER = LoggerFactory.getLogger("org.apache.james.CONFIGURATION");
private static final Logger LOGGER = LoggerFactory.getLogger(JamesServerMain.class);
--

Loggers can be hierarchical.
This helps when we build a logger from a class name, and we use package structure to drive the logger hierarchy.

The most useful loggers should to be documented below.
Please maintain this list of loggers.

org.apache.james.CONFIGURATION:: It is used to log events related to configuration loading and updating.
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,8 @@

import org.apache.james.filesystem.api.JamesDirectoriesProvider;

import com.google.common.base.MoreObjects;

public class JamesServerResourceLoader implements JamesDirectoriesProvider {

private final String rootDirectory;
Expand Down Expand Up @@ -58,4 +60,12 @@ public String getRootDirectory() {
return rootDirectory;
}

@Override
public String toString() {
return MoreObjects.toStringHelper(this)
.add("rootDirectory", rootDirectory)
.add("varDirectory", getVarDirectory())
.add("confDirectory", getConfDirectory())
.toString();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -134,6 +134,14 @@ public ConfigurationPath configurationPath() {
public JamesDirectoriesProvider directories() {
return directories;
}

@Override
public String toString() {
return MoreObjects.toStringHelper(this)
.add("configurationPath", configurationPath)
.add("directories", directories)
.toString();
}
}

static Basic.Builder builder() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -171,6 +171,7 @@ public static void main(String[] args) throws Exception {
.useWorkingDirectoryEnvProperty()
.build();

LOGGER.info("Loading configuration {}", configuration.toString());
GuiceJamesServer server = createServer(configuration)
.combineWith(new JMXServerModule());

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@ public static void main(String[] args) throws Exception {
.useWorkingDirectoryEnvProperty()
.build();

LOGGER.info("Loading configuration {}", configuration.toString());
GuiceJamesServer server = createServer(configuration)
.combineWith(new JMXServerModule());

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,7 @@ public static void main(String[] args) throws Exception {
.useWorkingDirectoryEnvProperty()
.build();

LOGGER.info("Loading configuration {}", configuration.toString());
GuiceJamesServer server = createServer(configuration)
.combineWith(new JMXServerModule());

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@ public static void main(String[] args) throws Exception {
.useWorkingDirectoryEnvProperty()
.build();

LOGGER.info("Loading configuration {}", configuration.toString());
GuiceJamesServer server = createServer(configuration)
.combineWith(new JMXServerModule());

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -35,13 +35,16 @@
import org.apache.commons.configuration2.ex.ConfigurationException;
import org.apache.james.filesystem.api.FileSystem;
import org.apache.james.server.core.configuration.Configuration.ConfigurationPath;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import com.google.common.base.Joiner;
import com.google.common.base.Preconditions;
import com.google.common.base.Strings;

public class PropertiesProvider {

private static final Logger LOGGER = LoggerFactory.getLogger("org.apache.james.CONFIGURATION");
private static final char COMMA = ',';
private static final String COMMA_STRING = ",";

Expand Down Expand Up @@ -85,7 +88,9 @@ private Configuration getConfiguration(File propertiesFile) throws Configuration

private Optional<File> getConfigurationFile(String fileName) {
try {
return Optional.of(fileSystem.getFile(configurationPrefix.asString() + fileName + ".properties"))
File file = fileSystem.getFile(configurationPrefix.asString() + fileName + ".properties");
LOGGER.info("Load configuration file {}", file.getAbsolutePath());
return Optional.of(file)
.filter(File::exists);
} catch (FileNotFoundException e) {
return Optional.empty();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,13 @@

package org.apache.james;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

public interface JamesServerMain {

Logger LOGGER = LoggerFactory.getLogger("org.apache.james.CONFIGURATION");

static void main(GuiceJamesServer server) throws Exception {
server.start();

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -101,6 +101,7 @@ public static void main(String[] args) throws Exception {
.useWorkingDirectoryEnvProperty()
.build();

LOGGER.info("Loading configuration {}", configuration.toString());
GuiceJamesServer server = createServer(configuration)
.combineWith(new JMXServerModule());

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,7 @@ public static void main(String[] args) throws Exception {
.useWorkingDirectoryEnvProperty()
.build();

LOGGER.info("Loading configuration {}", configuration.toString());
GuiceJamesServer server = createServer(configuration);

JamesServerMain.main(server);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -138,7 +138,7 @@ public static void main(String[] args) throws Exception {
.useWorkingDirectoryEnvProperty()
.build();


LOGGER.info("Loading configuration {}", configuration.toString());
GuiceJamesServer server = createServer(configuration)
.combineWith(new FakeSearchMailboxModule(), new JMXServerModule());

Expand Down

0 comments on commit b90d05e

Please sign in to comment.