Commit ccf58721 by Billy Larru

implementando log4j2

parent 9a44428e
...@@ -33,6 +33,8 @@ file.reference.c3p0-0.9.1.1.jar=librerias\\c3p0-0.9.1.1.jar ...@@ -33,6 +33,8 @@ file.reference.c3p0-0.9.1.1.jar=librerias\\c3p0-0.9.1.1.jar
file.reference.commons-dbcp-1.4.jar=librerias\\commons-dbcp-1.4.jar file.reference.commons-dbcp-1.4.jar=librerias\\commons-dbcp-1.4.jar
file.reference.commons-pool-1.6.jar=librerias\\commons-pool-1.6.jar file.reference.commons-pool-1.6.jar=librerias\\commons-pool-1.6.jar
file.reference.json-20151123.jar=librerias\\json-20151123.jar file.reference.json-20151123.jar=librerias\\json-20151123.jar
file.reference.log4j-api-2.3.jar=librerias\\log4j-api-2.3.jar
file.reference.log4j-core-2.3.jar=librerias\\log4j-core-2.3.jar
file.reference.mysql-connector-java-3.1.14-bin.jar=librerias\\mysql-connector-java-3.1.14-bin.jar file.reference.mysql-connector-java-3.1.14-bin.jar=librerias\\mysql-connector-java-3.1.14-bin.jar
includes=** includes=**
jar.compress=false jar.compress=false
...@@ -41,7 +43,9 @@ javac.classpath=\ ...@@ -41,7 +43,9 @@ javac.classpath=\
${file.reference.commons-dbcp-1.4.jar}:\ ${file.reference.commons-dbcp-1.4.jar}:\
${file.reference.commons-pool-1.6.jar}:\ ${file.reference.commons-pool-1.6.jar}:\
${file.reference.json-20151123.jar}:\ ${file.reference.json-20151123.jar}:\
${file.reference.mysql-connector-java-3.1.14-bin.jar} ${file.reference.mysql-connector-java-3.1.14-bin.jar}:\
${file.reference.log4j-api-2.3.jar}:\
${file.reference.log4j-core-2.3.jar}
# Space-separated list of extra javac options # Space-separated list of extra javac options
javac.compilerargs= javac.compilerargs=
javac.deprecation=false javac.deprecation=false
......
<?xml version="1.0" encoding="UTF-8"?>
<configuration status="error">
<!--The first definition of all appender-->
<appenders>
<!--The output console configuration-->
<Console name="Console" target="SYSTEM_OUT">
<!--The console output only level and above the level of the information (onMatch), directly to the other(onMismatch)-->
<ThresholdFilter level="trace" onMatch="ACCEPT" onMismatch="DENY"/>
<!--This all know is the output log format-->
<PatternLayout pattern="%d{HH:mm:ss.SSS} %-5level %class{36} %L %M - %msg%xEx%n"/>
</Console>
<!--Document will print out all the information, the log every time you run the program will automatically clear, determined by the append property, this is also very useful, suitable for temporary test-->
<File name="log" fileName="D:/Logs/log/test.log" append="false">
<PatternLayout pattern="%d{HH:mm:ss.SSS} %-5level %class{36} %L %M - %msg%xEx%n"/>
</File>
<!--This will print all the information, each size is more than size, then the size size of the log will automatically in the following year month was built according to the folder and file compression, as-->
<RollingFile name="RollingFile" fileName="D:/LogSalidasAutomaticas/traza.log"
filePattern="D:/Logs/errores-%d{MM-dd-yyyy}-%i.log.gz">
<PatternLayout pattern="%d{yyyy-MM-dd 'at' HH:mm:ss.S z} %-5level %class{36} %L %M - %msg%xEx%n"/>
<SizeBasedTriggeringPolicy size="50MB"/>
</RollingFile>
</appenders>
<!--Then the definition of logger, only the definition of logger and the introduction of the appender, the appender will take effect-->
<loggers>
<!--Create a default root logger-->
<root level="trace">
<appender-ref ref="RollingFile"/>
<appender-ref ref="Console"/>
</root>
<Logger name="org.hibernate" level="debug">
<appender-ref ref="Console"/>
</Logger>
<Logger name="org.hibernate.SQL" level="debug" >
<appender-ref ref="Console"/>
</Logger>
<logger name="org.hibernate.type" level="trace">
<appender-ref ref="Console"/>
</logger>
</loggers>
</configuration>
...@@ -3,13 +3,22 @@ package salidasautomaticas.main; ...@@ -3,13 +3,22 @@ package salidasautomaticas.main;
import java.util.Date; import java.util.Date;
import java.util.Timer; import java.util.Timer;
import java.util.TimerTask; import java.util.TimerTask;
import org.apache.logging.log4j.Level;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import org.apache.logging.log4j.core.LoggerContext;
import org.apache.logging.log4j.core.config.Configuration;
import salidasautomaticas.schedule.ProgramarSalidasTask; import salidasautomaticas.schedule.ProgramarSalidasTask;
import salidasautomaticas.util.Metodos; import salidasautomaticas.util.Metodos;
public class Main { public class Main {
private static final Logger log = LogManager.getLogger(Main.class);
public static void main(String[] args){ public static void main(String[] args){
System.out.println("Inicializando programa..."); setLogLevel(Level.TRACE);
log.error("Inicializando programa...");
Date horaInicio = Metodos.getHoraEjecucion("12:00"); Date horaInicio = Metodos.getHoraEjecucion("12:00");
int periodo = 86400000;//El codigo se ejecutará cada 24h, 86400000 representa 24h en milisegundos int periodo = 86400000;//El codigo se ejecutará cada 24h, 86400000 representa 24h en milisegundos
...@@ -18,4 +27,13 @@ public class Main { ...@@ -18,4 +27,13 @@ public class Main {
timer.schedule(task , horaInicio, periodo); timer.schedule(task , horaInicio, periodo);
} }
public static void setLogLevel(Level l) {
log.info("Setting log level to " + l.name());
LoggerContext ctx = (LoggerContext) LogManager.getContext(false);
Configuration conf = ctx.getConfiguration();
conf.getLoggerConfig(LogManager.ROOT_LOGGER_NAME).setLevel(l);
ctx.updateLoggers(conf);
}
} }
...@@ -10,6 +10,7 @@ import java.time.LocalDate; ...@@ -10,6 +10,7 @@ import java.time.LocalDate;
import java.util.Iterator; import java.util.Iterator;
import java.util.logging.Level; import java.util.logging.Level;
import java.util.logging.Logger; import java.util.logging.Logger;
import org.apache.logging.log4j.LogManager;
import org.json.JSONArray; import org.json.JSONArray;
import org.json.JSONObject; import org.json.JSONObject;
import salidasautomaticas.dao.SalidasDAO; import salidasautomaticas.dao.SalidasDAO;
...@@ -20,7 +21,7 @@ import salidasautomaticas.util.EstandarDAO; ...@@ -20,7 +21,7 @@ import salidasautomaticas.util.EstandarDAO;
* @author sistem19user * @author sistem19user
*/ */
public class SalidasMysqlDAO implements SalidasDAO { public class SalidasMysqlDAO implements SalidasDAO {
private static final org.apache.logging.log4j.Logger log = LogManager.getLogger(SalidasMysqlDAO.class);
@Override @Override
public JSONArray obtenerHorariosSalida() throws Exception { public JSONArray obtenerHorariosSalida() throws Exception {
JSONArray jsonArray = new JSONArray(); JSONArray jsonArray = new JSONArray();
...@@ -158,9 +159,9 @@ public class SalidasMysqlDAO implements SalidasDAO { ...@@ -158,9 +159,9 @@ public class SalidasMysqlDAO implements SalidasDAO {
int aleatorioHora = 0, aleatorioMin = 0; int aleatorioHora = 0, aleatorioMin = 0;
JSONArray listaAsistencias = obtenerAsistencias(horaSalida); JSONArray listaAsistencias = obtenerAsistencias(horaSalida);
System.out.println("tamaño lista=" + listaAsistencias.length()); log.info("tamaño lista=" + listaAsistencias.length());
System.out.println("lista de asistencias para actualizar " + horaSalida + " " + listaAsistencias); log.info("lista de asistencias para actualizar " + horaSalida + " " + listaAsistencias);
int[] updateCounts = new int[listaAsistencias.length()]; int[] updateCounts = new int[listaAsistencias.length()];
boolean updateAll = false; boolean updateAll = false;
...@@ -192,7 +193,7 @@ public class SalidasMysqlDAO implements SalidasDAO { ...@@ -192,7 +193,7 @@ public class SalidasMysqlDAO implements SalidasDAO {
updateCounts[i] = resultado.getInt("msg"); updateCounts[i] = resultado.getInt("msg");
i++; i++;
System.out.println("Asistencia actualizada"); log.info("Asistencia actualizada");
} }
return resultado; return resultado;
......
...@@ -12,6 +12,7 @@ import java.util.Timer; ...@@ -12,6 +12,7 @@ import java.util.Timer;
import java.util.TimerTask; import java.util.TimerTask;
import java.util.logging.Level; import java.util.logging.Level;
import java.util.logging.Logger; import java.util.logging.Logger;
import org.apache.logging.log4j.LogManager;
import org.json.JSONArray; import org.json.JSONArray;
import org.json.JSONObject; import org.json.JSONObject;
import salidasautomaticas.services.SalidasServices; import salidasautomaticas.services.SalidasServices;
...@@ -22,6 +23,7 @@ import salidasautomaticas.util.Metodos; ...@@ -22,6 +23,7 @@ import salidasautomaticas.util.Metodos;
* @author sistem19user * @author sistem19user
*/ */
public class ProgramarSalidasTask extends TimerTask { public class ProgramarSalidasTask extends TimerTask {
private static final org.apache.logging.log4j.Logger log = LogManager.getLogger(ProgramarSalidasTask.class);
private SalidasServices service = new SalidasServices(); private SalidasServices service = new SalidasServices();
@Override @Override
...@@ -44,24 +46,24 @@ public class ProgramarSalidasTask extends TimerTask { ...@@ -44,24 +46,24 @@ public class ProgramarSalidasTask extends TimerTask {
JSONObject obj = (JSONObject) it.next(); JSONObject obj = (JSONObject) it.next();
String horaSalida = obj.getString("salida"); String horaSalida = obj.getString("salida");
System.out.println("Leyendo hora de salida " + horaSalida); log.info("Leyendo hora de salida " + horaSalida);
if (validarProgramacionSalida(horaSalida)) { if (validarProgramacionSalida(horaSalida)) {
System.out.println("Programando hora de salida " + horaSalida); log.info("Programando hora de salida " + horaSalida);
Date horaEjecucion = Metodos.getHoraEjecucion(horaSalida); Date horaEjecucion = Metodos.getHoraEjecucion(horaSalida);
timer = new Timer(); timer = new Timer();
task = new ActualizarSalidaTask(timer, horaSalida); task = new ActualizarSalidaTask(timer, horaSalida);
timer.schedule(task, horaEjecucion); timer.schedule(task, horaEjecucion);
System.out.println("Timer " + timer); log.info("Timer " + timer);
} else { } else {
System.out.println("Ya pasó la hora"); log.info("Ya pasó la hora");
} }
System.out.println(); log.info("\n");
} }
} catch (Exception ex) { } catch (Exception ex) {
Logger.getLogger(ProgramarSalidasTask.class.getName()).log(Level.SEVERE, null, ex); log.error(ex);
} }
} }
......
package salidasautomaticas.services; package salidasautomaticas.services;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import org.json.JSONArray; import org.json.JSONArray;
import org.json.JSONObject; import org.json.JSONObject;
import salidasautomaticas.dao.DAOFactory; import salidasautomaticas.dao.DAOFactory;
import salidasautomaticas.dao.SalidasDAO; import salidasautomaticas.dao.SalidasDAO;
import salidasautomaticas.main.Main;
/** /**
* *
* @author sistem19user * @author sistem19user
*/ */
public class SalidasServices { public class SalidasServices {
private static final Logger log = LogManager.getLogger(SalidasServices.class);
DAOFactory daofactory = DAOFactory.getFactory(DAOFactory.MYSQL); DAOFactory daofactory = DAOFactory.getFactory(DAOFactory.MYSQL);
SalidasDAO salidadao = daofactory.getSalidasDAO(); SalidasDAO salidadao = daofactory.getSalidasDAO();
...@@ -20,7 +24,7 @@ public class SalidasServices { ...@@ -20,7 +24,7 @@ public class SalidasServices {
try { try {
listaHorarios = salidadao.obtenerHorariosSalida(); listaHorarios = salidadao.obtenerHorariosSalida();
} catch (Exception e) { } catch (Exception e) {
e.printStackTrace(); log.error(e);
} }
return listaHorarios; return listaHorarios;
...@@ -31,7 +35,7 @@ public class SalidasServices { ...@@ -31,7 +35,7 @@ public class SalidasServices {
try { try {
valor = salidadao.actualizarSalida(horaSalida); valor = salidadao.actualizarSalida(horaSalida);
} catch (Exception e) { } catch (Exception e) {
e.printStackTrace(); log.error(e);
} }
return valor; return valor;
} }
...@@ -41,7 +45,7 @@ public class SalidasServices { ...@@ -41,7 +45,7 @@ public class SalidasServices {
try { try {
valor = salidadao.asignarHorarioSalida(); valor = salidadao.asignarHorarioSalida();
} catch (Exception e) { } catch (Exception e) {
e.printStackTrace(); log.error(e);
} }
return valor; return valor;
} }
......
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