Skip to content
This repository has been archived by the owner on Nov 9, 2017. It is now read-only.

Commit

Permalink
Add configurable email logging appender.
Browse files Browse the repository at this point in the history
  • Loading branch information
Carlos Munoz committed Jul 6, 2012
1 parent fe19749 commit c47d996
Show file tree
Hide file tree
Showing 7 changed files with 338 additions and 13 deletions.
Expand Up @@ -40,14 +40,17 @@
public class HApplicationConfiguration extends ModelEntityBase
{

public static String KEY_HOST = "flies.host";
public static String KEY_REGISTER = "flies.register";
public static String KEY_DOMAIN = "flies.email.domain";
public static String KEY_ADMIN_EMAIL = "email.admin.addr";
public static String KEY_EMAIL_FROM_ADDRESS = "email.from.addr";
public static String KEY_HOME_CONTENT = "flies.home.content";
public static String KEY_HELP_CONTENT = "flies.help.content";
public static String KEY_LOGINCONFIG_URL = "zanata.login-config.url";
public static String KEY_HOST = "flies.host";
public static String KEY_REGISTER = "flies.register";
public static String KEY_DOMAIN = "flies.email.domain";
public static String KEY_ADMIN_EMAIL = "email.admin.addr";
public static String KEY_EMAIL_FROM_ADDRESS = "email.from.addr";
public static String KEY_HOME_CONTENT = "flies.home.content";
public static String KEY_HELP_CONTENT = "flies.help.content";
public static String KEY_LOGINCONFIG_URL = "zanata.login-config.url";
public static String KEY_LOG_DESTINATION_EMAIL = "zanata.log.destination.email";
public static String KEY_EMAIL_LOG_EVENTS = "zanata.log.email.active";
public static String KEY_EMAIL_LOG_LEVEL = "zanata.log.email.level";
private static final long serialVersionUID = 8652817113098817448L;

private String key;
Expand Down
16 changes: 14 additions & 2 deletions zanata-war/pom.xml
Expand Up @@ -312,8 +312,8 @@
</additionalClasspathElements>
<childDelegation>true</childDelegation>
<useSystemClassLoader>true</useSystemClassLoader>
<argLine xml:space="preserve">-Xmx1024m -XX:MaxPermSize=512m -XX:+UseConcMarkSweepGC
-XX:+CMSClassUnloadingEnabled -XX:+HeapDumpOnOutOfMemoryError
<argLine xml:space="preserve">-Xmx1024m -XX:MaxPermSize=512m -XX:+UseConcMarkSweepGC
-XX:+CMSClassUnloadingEnabled -XX:+HeapDumpOnOutOfMemoryError
-XX:HeapDumpPath=${project.build.directory} -Dsun.lang.ClassLoader.allowArraySyntax=true</argLine>
<classpathDependencyExcludes>
<classpathDependencyExclude>
Expand Down Expand Up @@ -1118,6 +1118,12 @@
<scope>test</scope>
</dependency>

<dependency>
<groupId>net.sourceforge.openutils</groupId>
<artifactId>openutils-log4j</artifactId>
<version>2.0.5</version>
</dependency>

<dependency>
<groupId>com.google.guava</groupId>
<artifactId>guava</artifactId>
Expand Down Expand Up @@ -1229,6 +1235,12 @@
<scope>provided</scope>
</dependency>

<dependency>
<groupId>javax.mail</groupId>
<artifactId>mail</artifactId>
<scope>provided</scope>
</dependency>

<dependency>
<groupId>org.hamcrest</groupId>
<artifactId>hamcrest-core</artifactId>
Expand Down
72 changes: 72 additions & 0 deletions zanata-war/src/main/java/org/zanata/ApplicationConfiguration.java
Expand Up @@ -27,6 +27,9 @@
import java.util.List;
import java.util.Map;

import org.apache.log4j.HTMLLayout;
import org.apache.log4j.Level;
import org.apache.log4j.Logger;
import org.jboss.seam.Component;
import org.jboss.seam.ScopeType;
import org.jboss.seam.annotations.Create;
Expand All @@ -38,6 +41,7 @@
import org.jboss.seam.log.Log;
import org.jboss.seam.log.Logging;
import org.zanata.dao.ApplicationConfigurationDAO;
import org.zanata.log4j.ZanataSMTPAppender;
import org.zanata.model.HApplicationConfiguration;
import org.zanata.security.AuthenticationType;

Expand All @@ -48,11 +52,14 @@
public class ApplicationConfiguration implements Serializable
{

private static final String EMAIL_APPENDER_NAME = "zanata.log.appender.email";
public static final String EVENT_CONFIGURATION_CHANGED = "zanata.configuration.changed";

private static final Log log = Logging.getLog(ApplicationConfiguration.class);
private static final long serialVersionUID = -4970657841198107092L;

private static final ZanataSMTPAppender smtpAppenderInstance = new ZanataSMTPAppender();

private Map<String, String> configValues = new HashMap<String, String>();

private boolean debug;
Expand Down Expand Up @@ -93,13 +100,48 @@ public void load()
}
}
this.configValues = configValues;

this.applyLoggingConfiguration();
}

private void setDefaults(Map<String, String> map)
{
map.put(HApplicationConfiguration.KEY_REGISTER, "/zanata/account/register");
map.put(HApplicationConfiguration.KEY_HOST, "http://localhost:8080/zanata");
map.put(HApplicationConfiguration.KEY_EMAIL_FROM_ADDRESS, "no-reply@redhat.com");
map.put(HApplicationConfiguration.KEY_EMAIL_LOG_LEVEL, "WARN");
}

/**
* Apply logging configuration.
*/
public void applyLoggingConfiguration()
{
final Logger rootLogger = Logger.getRootLogger();

if( isEmailLogAppenderEnabled() )
{
// NB: This appender uses Seam's email configuration (no need for host or port)
smtpAppenderInstance.setName(EMAIL_APPENDER_NAME);
smtpAppenderInstance.setFrom(getFromEmailAddr());
smtpAppenderInstance.setTo(this.configValues.get(HApplicationConfiguration.KEY_LOG_DESTINATION_EMAIL));
smtpAppenderInstance.setSubject("%p log message from Zanata at " + this.getServerPath());
smtpAppenderInstance.setLayout(new HTMLLayout());
//smtpAppenderInstance.setLayout(new PatternLayout("%-5p [%c] %m%n"));
smtpAppenderInstance.setThreshold(Level.toLevel(getEmailLogLevel()));
smtpAppenderInstance.setTimeout(60); // will aggregate identical messages within 60 sec periods
smtpAppenderInstance.activateOptions();

// Safe to add more than once
rootLogger.addAppender(smtpAppenderInstance);
log.info("Email log appender is enabled [level: " + smtpAppenderInstance.getThreshold().toString() + "]");
}
else
{
rootLogger.removeAppender(EMAIL_APPENDER_NAME);
log.info("Email log appender is disabled.");
}
log.error("This is an error", new Exception("error"));
}

public String getRegisterPath()
Expand Down Expand Up @@ -219,4 +261,34 @@ public boolean getEnableCopyTrans()
return enableCopyTrans;
}

public boolean isEmailLogAppenderEnabled()
{
String strVal = configValues.get(HApplicationConfiguration.KEY_EMAIL_LOG_EVENTS);

if(strVal == null)
{
return false;
}
else
{
return Boolean.parseBoolean( strVal );
}
}

public List<String> getLogDestinationEmails()
{
String s = configValues.get(HApplicationConfiguration.KEY_LOG_DESTINATION_EMAIL);
if (s == null || s.trim().length() == 0)
{
return new ArrayList<String>();
}
String[] ss = s.trim().split("\\s*,\\s*");
return new ArrayList<String>(Arrays.asList(ss));
}

public String getEmailLogLevel()
{
return configValues.get(HApplicationConfiguration.KEY_EMAIL_LOG_LEVEL);
}

}
Expand Up @@ -51,6 +51,9 @@ public class ServerConfigurationBean implements Serializable
*/
private static final long serialVersionUID = 1L;

@Logger
Log log;

@In
ApplicationConfigurationDAO applicationConfigurationDAO;

Expand All @@ -61,8 +64,9 @@ public class ServerConfigurationBean implements Serializable
private String fromEmailAddr;
private String homeContent;
private String helpContent;
@Logger
Log log;
private boolean enableLogEmail;
private String logDestinationEmails;
private String logEmailLevel;


public String getHomeContent()
Expand Down Expand Up @@ -197,6 +201,36 @@ public void setServerUrl(String serverUrl)
this.serverUrl = serverUrl;
}

public boolean isEnableLogEmail()
{
return enableLogEmail;
}

public void setEnableLogEmail(boolean enableLogEmail)
{
this.enableLogEmail = enableLogEmail;
}

public String getLogDestinationEmails()
{
return logDestinationEmails;
}

public void setLogDestinationEmails(String logDestinationEmails)
{
this.logDestinationEmails = logDestinationEmails;
}

public String getLogEmailLevel()
{
return logEmailLevel;
}

public void setLogEmailLevel(String logEmailLevel)
{
this.logEmailLevel = logEmailLevel;
}

@Create
public void onCreate()
{
Expand Down Expand Up @@ -225,7 +259,21 @@ public void onCreate()
{
this.fromEmailAddr = fromAddressValue.getValue();
}

HApplicationConfiguration emailLogEventsValue = applicationConfigurationDAO.findByKey(HApplicationConfiguration.KEY_EMAIL_LOG_EVENTS);
if (emailLogEventsValue != null)
{
this.enableLogEmail = Boolean.parseBoolean(emailLogEventsValue.getValue());
}
HApplicationConfiguration logDestinationValue = applicationConfigurationDAO.findByKey(HApplicationConfiguration.KEY_LOG_DESTINATION_EMAIL);
if (logDestinationValue != null)
{
this.logDestinationEmails = logDestinationValue.getValue();
}
HApplicationConfiguration logEmailLevelValue = applicationConfigurationDAO.findByKey(HApplicationConfiguration.KEY_EMAIL_LOG_LEVEL);
if(logEmailLevelValue != null)
{
this.logEmailLevel = logEmailLevelValue.getValue();
}
}

@Transactional
Expand Down Expand Up @@ -321,6 +369,53 @@ else if (fromEmailAddr != null && !fromEmailAddr.isEmpty())
applicationConfigurationDAO.makePersistent(fromEmailAddrValue);
}

HApplicationConfiguration emailLogEventsValue = applicationConfigurationDAO.findByKey(HApplicationConfiguration.KEY_EMAIL_LOG_EVENTS);
if( emailLogEventsValue == null )
{
emailLogEventsValue = new HApplicationConfiguration(HApplicationConfiguration.KEY_EMAIL_LOG_EVENTS, Boolean.toString(enableLogEmail));
}
else
{
emailLogEventsValue.setValue( Boolean.toString(enableLogEmail) );
}
applicationConfigurationDAO.makePersistent( emailLogEventsValue );

HApplicationConfiguration logDestEmailValue = applicationConfigurationDAO.findByKey(HApplicationConfiguration.KEY_LOG_DESTINATION_EMAIL);
if (logDestEmailValue != null)
{
if (logDestinationEmails == null || logDestinationEmails.isEmpty())
{
applicationConfigurationDAO.makeTransient(logDestEmailValue);
}
else
{
logDestEmailValue.setValue(logDestinationEmails);
}
}
else if (logDestinationEmails != null && !logDestinationEmails.isEmpty())
{
logDestEmailValue = new HApplicationConfiguration(HApplicationConfiguration.KEY_LOG_DESTINATION_EMAIL, logDestinationEmails);
applicationConfigurationDAO.makePersistent(logDestEmailValue);
}

HApplicationConfiguration logEmailLevelValue = applicationConfigurationDAO.findByKey(HApplicationConfiguration.KEY_EMAIL_LOG_LEVEL);
if (logEmailLevelValue != null)
{
if (logEmailLevel == null || logEmailLevel.isEmpty())
{
applicationConfigurationDAO.makeTransient(logEmailLevelValue);
}
else
{
logEmailLevelValue.setValue(logEmailLevel);
}
}
else if (logEmailLevel != null && !logEmailLevel.isEmpty())
{
logEmailLevelValue = new HApplicationConfiguration(HApplicationConfiguration.KEY_EMAIL_LOG_LEVEL, logEmailLevel);
applicationConfigurationDAO.makePersistent(logEmailLevelValue);
}

applicationConfigurationDAO.flush();
FacesMessages.instance().add("Configuration was successfully updated.");
if (Events.exists())
Expand Down

0 comments on commit c47d996

Please sign in to comment.