Commit b8e64149 authored by aninhacostaribeiro's avatar aninhacostaribeiro
Browse files

Changed Log system for Log4j

parent 94f741a7
# Root logger option
log4j.rootLogger=DEBUG, file
# Direct log messages to a log file
log4j.appender.file=org.apache.log4j.RollingFileAppender
log4j.appender.file.File=/var/log/fogbow-green-sitter/fogbow-green-sitter.log
log4j.appender.file.MaxFileSize=10MB
log4j.appender.file.MaxBackupIndex=10
log4j.appender.file.layout=org.apache.log4j.PatternLayout
log4j.appender.file.layout.ConversionPattern=%d{yyyy-MM-dd HH:mm:ss} %-5p %c{1}:%L - %m%n
# Different log levels for restlet and http-client
log4j.category.org.restlet=INFO
log4j.category.org.apache.http=INFO
\ No newline at end of file
......@@ -52,6 +52,25 @@
</execution>
</executions>
</plugin>
<plugin>
<groupId>org.codehaus.mojo</groupId>
<artifactId>exec-maven-plugin</artifactId>
<version>1.2.1</version>
<configuration>
<executable>java</executable>
<arguments>
<argument>-Dlog4j.configuration=file:log4j.properties</argument>
<argument>-cp</argument>
</arguments>
</configuration>
<executions>
<execution>
<goals>
<goal>exec</goal>
</goals>
</execution>
</executions>
</plugin>
</plugins>
</build>
<dependencies>
......
......@@ -4,14 +4,12 @@ import java.util.Properties;
import java.util.concurrent.Executors;
import java.util.concurrent.ScheduledExecutorService;
import java.util.concurrent.TimeUnit;
import java.util.logging.Level;
import java.util.logging.Logger;
import org.apache.log4j.Logger;
import org.dom4j.tree.DefaultElement;
import org.jamppa.client.XMPPClient;
import org.jamppa.client.plugin.xep0077.XEP0077;
import org.jivesoftware.smack.PacketListener;
import org.jivesoftware.smack.XMPPException;
import org.jivesoftware.smack.filter.PacketFilter;
import org.xmpp.packet.IQ;
import org.xmpp.packet.Packet;
......@@ -21,6 +19,8 @@ import org.xmpp.packet.IQ.Type;
public class AgentCommunicationComponent {
private static final String NAMESPACE = "org.fogbowcloud.green.IAmAlive";
private static final Logger LOGGER = Logger.getLogger(AgentCommunicationComponent.class);
private ScheduledExecutorService executor = Executors
.newScheduledThreadPool(1);
private Properties prop;
......@@ -28,12 +28,16 @@ public class AgentCommunicationComponent {
private XMPPClient client;
public AgentCommunicationComponent(Properties prop) {
try{
this.prop = prop;
this.client = new XMPPClient(this.prop.getProperty("xmpp.jid"),
this.prop.getProperty("xmpp.password"),
this.prop.getProperty("xmpp.host"), Integer.parseInt(this.prop
.getProperty("xmpp.port")));
this.sleepingTime = Long.parseLong(this.prop.getProperty("green.sleepingTime"));
}catch (Exception e){
LOGGER.fatal("The configuration file is not correct" + e);
}
}
public Boolean init() {
......@@ -45,10 +49,10 @@ public class AgentCommunicationComponent {
this.prop.getProperty("xmpp.password"));
this.client.login();
this.client.process(false);
LOGGER.info("connected to the server");
} catch (XMPPException e) {
Logger logger = Logger.getLogger("green.Agent");
logger.log(Level.SEVERE, "It was not possible to connect");
} catch (Exception e) {
LOGGER.fatal("It was not possible to connect to server " + e);
return false;
}
client.getConnection().addPacketListener(new PacketListener() {
......
......@@ -3,11 +3,13 @@ package org.fogbowcloud.green.agent.core;
import java.io.FileInputStream;
import java.io.IOException;
import java.util.Properties;
import java.util.logging.Level;
import java.util.logging.Logger;
import org.apache.log4j.ConsoleAppender;
import org.apache.log4j.Logger;
public class Main {
private static final Logger LOGGER = Logger.getLogger(Main.class);
public static Properties getProp(String path) throws IOException {
Properties props = new Properties();
FileInputStream file = new FileInputStream(path);
......@@ -15,7 +17,15 @@ public class Main {
return props;
}
private static void configureLog4j() {
ConsoleAppender console = new ConsoleAppender();
console.setThreshold(org.apache.log4j.Level.OFF);
console.activateOptions();
Logger.getRootLogger().addAppender(console);
}
public static void main(String[] args) {
configureLog4j();
try {
Properties prop = getProp(args[0]);
AgentCommunicationComponent acc = new AgentCommunicationComponent(prop);
......@@ -23,9 +33,8 @@ public class Main {
return;
acc.start();
} catch (Exception e) {
Logger logger = Logger.getLogger("green.agent");
logger.log(Level.SEVERE,"You must provide as parameter the"
+ " path for the configuration file");
LOGGER.fatal("You must provide as parameter the"
+ " path for the configuration file", e);
}
}
}
......@@ -3,13 +3,15 @@ package org.fogbowcloud.green.server.core;
import java.io.FileInputStream;
import java.io.IOException;
import java.util.Properties;
import java.util.logging.Level;
import java.util.logging.Logger;
import org.apache.log4j.Logger;
import org.apache.log4j.ConsoleAppender;
import org.fogbowcloud.green.server.communication.ServerCommunicationComponent;
import org.fogbowcloud.green.server.core.greenStrategy.DefaultGreenStrategy;
public class Main {
private static final Logger LOGGER = Logger.getLogger(Main.class);
public static Properties getProp(String path) throws IOException {
Properties props = new Properties();
......@@ -18,7 +20,15 @@ public class Main {
return props;
}
private static void configureLog4j() {
ConsoleAppender console = new ConsoleAppender();
console.setThreshold(org.apache.log4j.Level.OFF);
console.activateOptions();
Logger.getRootLogger().addAppender(console);
}
public static void main(String[] args) {
configureLog4j();
try {
Properties prop = getProp(args[0]);
DefaultGreenStrategy gs = new DefaultGreenStrategy(prop);
......@@ -26,11 +36,11 @@ public class Main {
prop, gs);
gs.setCommunicationComponent(scc);
gs.start();
LOGGER.info("Green Server started");
scc.process(true);
} catch (Exception e) {
Logger logger = Logger.getLogger("green.server");
logger.log(Level.WARNING, "You must provide as parameter the"
+ " path for the configuration file");
LOGGER.fatal("You must provide as parameter the"
+ " path for the configuration file", e);
}
}
}
......@@ -9,11 +9,14 @@ import java.util.concurrent.Executors;
import java.util.concurrent.ScheduledExecutorService;
import java.util.concurrent.TimeUnit;
import org.apache.log4j.Logger;
import org.fogbowcloud.green.server.communication.ServerCommunicationComponent;
import org.fogbowcloud.green.server.core.plugins.CloudInfoPlugin;
import org.fogbowcloud.green.server.core.plugins.openstack.OpenStackInfoPlugin;
public class DefaultGreenStrategy implements GreenStrategy {
private static final Logger LOGGER = Logger.getLogger(DefaultGreenStrategy.class);
private CloudInfoPlugin openStackPlugin;
private List<? extends Host> allWakedHosts;
......@@ -49,6 +52,9 @@ public class DefaultGreenStrategy implements GreenStrategy {
this.allWakedHosts = this.openStackPlugin.getHostInformation();
}
/*
* Constructor used for tests
* */
protected DefaultGreenStrategy(CloudInfoPlugin openStackPlugin,
long graceTime) {
this.openStackPlugin = openStackPlugin;
......@@ -140,6 +146,7 @@ public class DefaultGreenStrategy implements GreenStrategy {
aux.addAll(this.allWakedHosts);
aux.add(host);
this.allWakedHosts = aux;
LOGGER.info("Host " + host.getName() + " was found");
}
}
......@@ -171,6 +178,7 @@ public class DefaultGreenStrategy implements GreenStrategy {
*/
if (nowTime - host.getNappingSince() > this.graceTime) {
scc.sendIdleHostToBed(host.getMacAddress());
LOGGER.info("Host " + host.getName() + " was sent to bed");
this.sleepingHosts.add(host);
}
}
......@@ -194,6 +202,7 @@ public class DefaultGreenStrategy implements GreenStrategy {
this.nappingHosts.remove(host);
}
this.lostHosts.add(host);
LOGGER.info("Host " + host.getName() + " was found");
}
}
for (Host host: this.lostHosts) {
......@@ -223,6 +232,7 @@ public class DefaultGreenStrategy implements GreenStrategy {
@Override
public void run() {
checkHostsLastSeen();
LOGGER.info("Checked when hosts were seen");
}
}, 0, lostHostTime, TimeUnit.MILLISECONDS);
......@@ -230,6 +240,7 @@ public class DefaultGreenStrategy implements GreenStrategy {
@Override
public void run() {
sendIdleHostsToBed();
LOGGER.info("Sent idle hosts to bed");
}
}, 0, sleepingTime, TimeUnit.MILLISECONDS);
}
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment