Merge pull request #13 from P0nk/standardized-logging
Set up standandarized logging with Slf4j + Log4j2
This commit is contained in:
34
pom.xml
34
pom.xml
@@ -15,6 +15,8 @@
|
||||
<maven.compiler.source>1.8</maven.compiler.source>
|
||||
<maven.compiler.target>1.8</maven.compiler.target>
|
||||
<mainClass>net.server.Server</mainClass>
|
||||
|
||||
<log4j.version>2.14.1</log4j.version>
|
||||
</properties>
|
||||
|
||||
<dependencies>
|
||||
@@ -33,21 +35,33 @@
|
||||
<artifactId>mysql-connector-java</artifactId>
|
||||
<version>8.0.23</version>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>org.slf4j</groupId>
|
||||
<artifactId>slf4j-api</artifactId>
|
||||
<version>1.7.21</version>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>org.slf4j</groupId>
|
||||
<artifactId>slf4j-jdk14</artifactId>
|
||||
<version>1.7.5</version>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>com.esotericsoftware.yamlbeans</groupId>
|
||||
<artifactId>yamlbeans</artifactId>
|
||||
<version>1.13</version>
|
||||
</dependency>
|
||||
|
||||
<!-- Logging -->
|
||||
<dependency>
|
||||
<groupId>org.slf4j</groupId>
|
||||
<artifactId>slf4j-api</artifactId>
|
||||
<version>1.7.30</version>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>org.apache.logging.log4j</groupId>
|
||||
<artifactId>log4j-core</artifactId>
|
||||
<version>${log4j.version}</version>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>org.apache.logging.log4j</groupId>
|
||||
<artifactId>log4j-api</artifactId>
|
||||
<version>${log4j.version}</version>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>org.apache.logging.log4j</groupId>
|
||||
<artifactId>log4j-slf4j-impl</artifactId>
|
||||
<version>${log4j.version}</version>
|
||||
</dependency>
|
||||
</dependencies>
|
||||
|
||||
<build>
|
||||
|
||||
@@ -59,6 +59,8 @@ import org.apache.mina.core.service.IoAcceptor;
|
||||
import org.apache.mina.core.session.IdleStatus;
|
||||
import org.apache.mina.filter.codec.ProtocolCodecFilter;
|
||||
import org.apache.mina.transport.socket.nio.NioSocketAcceptor;
|
||||
import org.slf4j.Logger;
|
||||
import org.slf4j.LoggerFactory;
|
||||
import server.CashShop.CashItemFactory;
|
||||
import server.MapleSkillbookInformationProvider;
|
||||
import server.ThreadManager;
|
||||
@@ -84,7 +86,7 @@ import java.util.concurrent.atomic.AtomicLong;
|
||||
import java.util.concurrent.locks.Lock;
|
||||
|
||||
public class Server {
|
||||
|
||||
private static final Logger log = LoggerFactory.getLogger(Server.class);
|
||||
private static Server instance = null;
|
||||
|
||||
public static Server getInstance() {
|
||||
@@ -385,7 +387,7 @@ public class Server {
|
||||
wldRLock.unlock();
|
||||
}
|
||||
|
||||
System.out.println("Starting world " + i);
|
||||
log.info("Starting world {}", i);
|
||||
|
||||
int exprate = YamlConfig.config.worlds.get(i).exp_rate;
|
||||
int mesorate = YamlConfig.config.worlds.get(i).meso_rate;
|
||||
@@ -431,10 +433,10 @@ public class Server {
|
||||
if (canDeploy) {
|
||||
world.setServerMessage(YamlConfig.config.worlds.get(i).server_message);
|
||||
|
||||
System.out.println("Finished loading world " + i + "\r\n");
|
||||
log.info("Finished loading world {}", i);
|
||||
return i;
|
||||
} else {
|
||||
System.out.println("Could not load world " + i + "...\r\n");
|
||||
log.error("Could not load world {}...", i);
|
||||
world.shutdown();
|
||||
return -2;
|
||||
}
|
||||
@@ -815,7 +817,7 @@ public class Server {
|
||||
}
|
||||
|
||||
public void init() {
|
||||
System.out.println("Cosmic v" + ServerConstants.VERSION + " starting up.\r\n");
|
||||
log.info("Cosmic v{} starting up.", ServerConstants.VERSION);
|
||||
|
||||
if (YamlConfig.config.server.SHUTDOWNHOOK) {
|
||||
Runtime.getRuntime().addShutdownHook(new Thread(shutdown(false)));
|
||||
@@ -853,16 +855,19 @@ public class Server {
|
||||
|
||||
long timeToTake = System.currentTimeMillis();
|
||||
SkillFactory.loadAllSkills();
|
||||
System.out.println("Skills loaded in " + ((System.currentTimeMillis() - timeToTake) / 1000.0) + " seconds");
|
||||
final double skillLoadTime = (System.currentTimeMillis() - timeToTake) / 1000.0;
|
||||
log.info("Skills loaded in {} seconds", skillLoadTime);
|
||||
|
||||
timeToTake = System.currentTimeMillis();
|
||||
|
||||
CashItemFactory.getSpecialCashItems();
|
||||
System.out.println("Items loaded in " + ((System.currentTimeMillis() - timeToTake) / 1000.0) + " seconds");
|
||||
final double itemLoadTime = (System.currentTimeMillis() - timeToTake) / 1000.0;
|
||||
log.info("Items loaded in {} seconds", itemLoadTime);
|
||||
|
||||
timeToTake = System.currentTimeMillis();
|
||||
MapleQuest.loadAllQuest();
|
||||
System.out.println("Quest loaded in " + ((System.currentTimeMillis() - timeToTake) / 1000.0) + " seconds\r\n");
|
||||
final double questLoadTime = (System.currentTimeMillis() - timeToTake) / 1000.0;
|
||||
log.info("Quest loaded in {} seconds", questLoadTime);
|
||||
|
||||
NewYearCardRecord.startPendingNewYearCardRequests();
|
||||
|
||||
@@ -882,20 +887,17 @@ public class Server {
|
||||
loadPlayerNpcMapStepFromDb();
|
||||
} catch (Exception e) {
|
||||
e.printStackTrace();//For those who get errors
|
||||
System.out.println("[SEVERE] Syntax error in 'world.ini'.");
|
||||
log.error("[SEVERE] Syntax error in 'world.ini'.");
|
||||
System.exit(0);
|
||||
}
|
||||
|
||||
System.out.println();
|
||||
|
||||
if (YamlConfig.config.server.USE_FAMILY_SYSTEM) {
|
||||
timeToTake = System.currentTimeMillis();
|
||||
MapleFamily.loadAllFamilies();
|
||||
System.out.println("Families loaded in " + ((System.currentTimeMillis() - timeToTake) / 1000.0) + " seconds\r\n");
|
||||
final double familyLoadTime = (System.currentTimeMillis() - timeToTake) / 1000.0;
|
||||
log.info("Families loaded in {} seconds", familyLoadTime);
|
||||
}
|
||||
|
||||
System.out.println();
|
||||
|
||||
IoBuffer.setUseDirectBuffer(false); // join IO operations performed by lxconan
|
||||
IoBuffer.setAllocator(new SimpleBufferAllocator());
|
||||
acceptor = new NioSocketAcceptor();
|
||||
@@ -908,9 +910,9 @@ public class Server {
|
||||
ex.printStackTrace();
|
||||
}
|
||||
|
||||
System.out.println("Listening on port 8484\r\n\r\n");
|
||||
log.info("Listening on port 8484");
|
||||
|
||||
System.out.println("Cosmic is now online.\r\n");
|
||||
log.info("Cosmic is now online.");
|
||||
online = true;
|
||||
|
||||
MapleSkillbookInformationProvider.getInstance();
|
||||
|
||||
@@ -21,42 +21,24 @@ along with this program. If not, see <http://www.gnu.org/licenses/>.
|
||||
*/
|
||||
package net.server.channel;
|
||||
|
||||
import java.io.File;
|
||||
import java.net.InetSocketAddress;
|
||||
import java.util.ArrayList;
|
||||
import java.util.LinkedList;
|
||||
import java.util.HashMap;
|
||||
import java.util.List;
|
||||
import java.util.Collections;
|
||||
import java.util.HashSet;
|
||||
import java.util.Map;
|
||||
import java.util.Map.Entry;
|
||||
import java.util.Set;
|
||||
import java.util.WeakHashMap;
|
||||
import java.util.concurrent.ScheduledFuture;
|
||||
|
||||
import net.server.audit.locks.MonitoredReadLock;
|
||||
import net.server.audit.locks.MonitoredWriteLock;
|
||||
import net.server.audit.locks.factory.MonitoredReadLockFactory;
|
||||
import net.server.audit.locks.factory.MonitoredWriteLockFactory;
|
||||
|
||||
import client.MapleCharacter;
|
||||
import config.YamlConfig;
|
||||
import net.server.audit.LockCollector;
|
||||
import net.server.audit.locks.MonitoredLockType;
|
||||
import net.server.audit.locks.MonitoredReentrantLock;
|
||||
import net.server.audit.locks.MonitoredReentrantReadWriteLock;
|
||||
import net.server.audit.locks.factory.MonitoredReentrantLockFactory;
|
||||
|
||||
import constants.game.GameConstants;
|
||||
import net.MapleServerHandler;
|
||||
import net.mina.MapleCodecFactory;
|
||||
|
||||
import net.server.PlayerStorage;
|
||||
import net.server.Server;
|
||||
|
||||
import net.server.world.World;
|
||||
import net.server.audit.LockCollector;
|
||||
import net.server.audit.locks.*;
|
||||
import net.server.audit.locks.factory.MonitoredReadLockFactory;
|
||||
import net.server.audit.locks.factory.MonitoredReentrantLockFactory;
|
||||
import net.server.audit.locks.factory.MonitoredWriteLockFactory;
|
||||
import net.server.services.BaseService;
|
||||
import net.server.services.ServicesManager;
|
||||
import net.server.services.type.ChannelServices;
|
||||
import net.server.world.MapleParty;
|
||||
import net.server.world.MaplePartyCharacter;
|
||||
|
||||
import net.server.world.World;
|
||||
import org.apache.mina.core.buffer.IoBuffer;
|
||||
import org.apache.mina.core.buffer.SimpleBufferAllocator;
|
||||
import org.apache.mina.core.filterchain.IoFilter;
|
||||
@@ -65,26 +47,25 @@ import org.apache.mina.core.session.IdleStatus;
|
||||
import org.apache.mina.filter.codec.ProtocolCodecFilter;
|
||||
import org.apache.mina.transport.socket.SocketSessionConfig;
|
||||
import org.apache.mina.transport.socket.nio.NioSocketAcceptor;
|
||||
|
||||
import client.MapleCharacter;
|
||||
import constants.game.GameConstants;
|
||||
import net.server.services.ServicesManager;
|
||||
import net.server.services.BaseService;
|
||||
import net.server.services.type.ChannelServices;
|
||||
import org.slf4j.Logger;
|
||||
import org.slf4j.LoggerFactory;
|
||||
import scripting.event.EventScriptManager;
|
||||
import server.TimerManager;
|
||||
import server.events.gm.MapleEvent;
|
||||
import server.expeditions.MapleExpedition;
|
||||
import server.expeditions.MapleExpeditionType;
|
||||
import server.maps.MapleHiredMerchant;
|
||||
import server.maps.MapleMap;
|
||||
import server.maps.MapleMapManager;
|
||||
import server.maps.MapleMiniDungeon;
|
||||
import server.maps.MapleMiniDungeonInfo;
|
||||
import server.maps.*;
|
||||
import tools.MaplePacketCreator;
|
||||
import tools.Pair;
|
||||
|
||||
import java.io.File;
|
||||
import java.net.InetSocketAddress;
|
||||
import java.util.*;
|
||||
import java.util.Map.Entry;
|
||||
import java.util.concurrent.ScheduledFuture;
|
||||
|
||||
public final class Channel {
|
||||
private static final Logger log = LoggerFactory.getLogger(Channel.class);
|
||||
|
||||
private int port = 7575;
|
||||
private PlayerStorage players = new PlayerStorage();
|
||||
@@ -173,7 +154,7 @@ public final class Channel {
|
||||
|
||||
services = new ServicesManager(ChannelServices.OVERALL);
|
||||
|
||||
System.out.println(" Channel " + getId() + ": Listening on port " + port);
|
||||
log.info("Channel {}: Listening on port {}", getId(), port);
|
||||
} catch (Exception e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
|
||||
@@ -1,22 +1,24 @@
|
||||
package tools;
|
||||
|
||||
import com.zaxxer.hikari.HikariConfig;
|
||||
import com.zaxxer.hikari.HikariDataSource;
|
||||
import config.YamlConfig;
|
||||
import org.slf4j.Logger;
|
||||
import org.slf4j.LoggerFactory;
|
||||
|
||||
import java.sql.Connection;
|
||||
import java.sql.SQLException;
|
||||
import java.time.Duration;
|
||||
import java.time.Instant;
|
||||
import java.util.concurrent.TimeUnit;
|
||||
|
||||
import com.zaxxer.hikari.HikariConfig;
|
||||
import com.zaxxer.hikari.HikariDataSource;
|
||||
|
||||
import config.YamlConfig;
|
||||
|
||||
/**
|
||||
* @author Frz (Big Daddy)
|
||||
* @author The Real Spookster - some modifications to this beautiful code
|
||||
* @author Ronan - some connection pool to this beautiful code
|
||||
*/
|
||||
public class DatabaseConnection {
|
||||
private static final Logger log = LoggerFactory.getLogger(DatabaseConnection.class);
|
||||
private static HikariDataSource dataSource;
|
||||
|
||||
public static Connection getConnection() throws SQLException {
|
||||
@@ -61,17 +63,17 @@ public class DatabaseConnection {
|
||||
* @return true if connection to the database initiated successfully, false if not successful
|
||||
*/
|
||||
public static boolean initializeConnectionPool() {
|
||||
System.out.println("Initializing connection pool...");
|
||||
log.info("Initializing connection pool...");
|
||||
final HikariConfig config = getConfig();
|
||||
Instant initStart = Instant.now();
|
||||
try {
|
||||
dataSource = new HikariDataSource(config);
|
||||
long initDuration = Duration.between(initStart, Instant.now()).toMillis();
|
||||
System.out.printf("Connection pool initialized in %d ms%n", initDuration);
|
||||
log.info("Connection pool initialized in {} ms", initDuration);
|
||||
return true;
|
||||
} catch (Exception e) {
|
||||
long timeout = Duration.between(initStart, Instant.now()).getSeconds();
|
||||
System.err.printf("Failed to initialize database connection pool. Gave up after %d seconds.%n", timeout);
|
||||
log.error("Failed to initialize database connection pool. Gave up after {} seconds.", timeout);
|
||||
}
|
||||
|
||||
// Timed out - failed to initialize
|
||||
|
||||
30
src/main/resources/log4j2.xml
Normal file
30
src/main/resources/log4j2.xml
Normal file
@@ -0,0 +1,30 @@
|
||||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
<Configuration status="WARN" name="Cosmic">
|
||||
<Properties>
|
||||
<Property name="filename">cosmic-log</Property>
|
||||
</Properties>
|
||||
<Appenders>
|
||||
<Console name="Console" target="SYSTEM_OUT">
|
||||
<PatternLayout pattern="%d{HH:mm:ss.SSS} [%t] %-5level %logger{36} - %msg%n"/>
|
||||
</Console>
|
||||
<RollingFile name="File"
|
||||
fileName="logs/${filename}.log"
|
||||
filePattern="logs/$${date:yyyy-MM}/$${date:yyyy-MM-dd}/${filename}-%d{yyyy-MM-dd_HH-mm-ss}-%i.log">
|
||||
<PatternLayout>
|
||||
<Pattern>%d{HH:mm:ss.SSS} [%t] %-5level %logger{36} - %msg%n</Pattern>
|
||||
</PatternLayout>
|
||||
<Policies>
|
||||
<OnStartupTriggeringPolicy minSize="0"/>
|
||||
<SizeBasedTriggeringPolicy size="20 MB"/>
|
||||
</Policies>
|
||||
</RollingFile>
|
||||
</Appenders>
|
||||
<Loggers>
|
||||
<Root level="trace">
|
||||
<AppenderRef ref="File" level="debug"/>
|
||||
<AppenderRef ref="Console" level="trace"/>
|
||||
</Root>
|
||||
<Logger name="org.apache.mina" level="info"/>
|
||||
<Logger name="com.zaxxer.hikari" level="info"/>
|
||||
</Loggers>
|
||||
</Configuration>
|
||||
Reference in New Issue
Block a user