Java-Gaming.org Hi !
Featured games (91)
games approved by the League of Dukes
Games in Showcase (764)
Games in Android Showcase (229)
games submitted by our members
Games in WIP (852)
games currently in development
News: Read the Java Gaming Resources, or peek at the official Java tutorials
 
    Home     Help   Search   Login   Register   
Pages: [1]
  ignore  |  Print  
  Write log files  (Read 12178 times)
0 Members and 1 Guest are viewing this topic.
Offline temu

Junior Devvie


Medals: 2
Exp: 2 years



« Posted 2014-06-27 12:54:26 »

Hi guys,
I want to write log files (.txt) to keep trace of what happens in my game server. But I don't know how this effectively works. If I am correct the server will write the file real time while it is also elaborating client requests like movement etc Isn't this going to make server performace worse?
Can you please tell me the right approach or suggest me any library or similar utility?

Offline DarkCart

JGO Kernel


Medals: 123
Projects: 9
Exp: 50 years


It's all in the mind, y'know.


« Reply #1 - Posted 2014-06-27 13:31:36 »

Well, standalone Java 1.8 has the java.io package, that you could use. It's got goodies such as PrintWriter, ObjectOutputStream, and ObjectInputStream, among others. The API documentation is here:
http://docs.oracle.com/javase/7/docs/api/java/io/package-summary.html

The darkest of carts.
Offline temu

Junior Devvie


Medals: 2
Exp: 2 years



« Reply #2 - Posted 2014-06-27 13:33:52 »

I know them but are they optimized to avoid worse performance?
Games published by our own members! Check 'em out!
Legends of Yore - The Casual Retro Roguelike
Offline BurntPizza

« JGO Bitwise Duke »


Medals: 485
Exp: 7 years



« Reply #3 - Posted 2014-06-27 13:36:02 »

I know them but are they optimized to avoid worse performance?

They're in the standard library. There's your answer.

Tip: don't optimize until you need to.
Offline 65K
« Reply #4 - Posted 2014-06-27 13:53:06 »

Use a logging library which allows to enable logging as needed.

Lethal Running - a RPG about a deadly game show held in a futuristic dystopian society.
Offline temu

Junior Devvie


Medals: 2
Exp: 2 years



« Reply #5 - Posted 2014-06-27 14:14:34 »

Use a logging library which allows to enable logging as needed.
Probably the best idea
Offline Longarmx
« Reply #6 - Posted 2014-06-27 15:55:53 »

If you just want very simple functionality, then you can just call this at the start of your program,
1  
2  
3  
4  
5  
6  
7  
8  
9  
10  
try
{
   PrintStream out = new PrintStream("path to log here.txt");
   System.setOut(out);
   System.setErr(out);
}
catch (FileNotFoundException e)
{
   e.printStackTrace();
}


Anytime you then print out with System.out.println() or System.err.println(), then it will write a line to the log.

Offline temu

Junior Devvie


Medals: 2
Exp: 2 years



« Reply #7 - Posted 2014-06-27 16:31:05 »

If you just want very simple functionality, then you can just call this at the start of your program,
1  
2  
3  
4  
5  
6  
7  
8  
9  
10  
try
{
   PrintStream out = new PrintStream("path to log here.txt");
   System.setOut(out);
   System.setErr(out);
}
catch (FileNotFoundException e)
{
   e.printStackTrace();
}


Anytime you then print out with System.out.println() or System.err.println(), then it will write a line to the log.

Uh seems very good, I like your solution, gonna try in the code. Thanks a lot Wink
Offline ClaasJG

JGO Coder


Medals: 42



« Reply #8 - Posted 2014-06-27 16:39:22 »

1  
java -jar myjar.jar > output.txt

Will redirect the output too.
I guess since you are talking about a server this is possibly.

[...]You need
1  
java -jar myjar.jar > output.txt 2>&1
if you want both stdout and stderr. Assuming Windows, of course.

My english has to be tweaked. Please show me my mistakes.
Offline BurntPizza

« JGO Bitwise Duke »


Medals: 485
Exp: 7 years



« Reply #9 - Posted 2014-06-27 16:44:09 »

1  
java -jar myjar.jar > output.txt

Will redirect the output too.
I guess since you are talking about a server this is possibly.

You need
java -jar myjar.jar > output.txt 2>&1
if you want both stdout and stderr. Assuming Windows, of course.

Games published by our own members! Check 'em out!
Legends of Yore - The Casual Retro Roguelike
Offline klaus
« Reply #10 - Posted 2014-09-07 08:10:01 »

It's a bad idea to reinvent the wheel. There are already really good and fast logging libraries. As with every library it will take you some (concerning logging libraries not much) time to get the hang of it, but it will serve you well in the future.

I personally can recommend SLF4J combined with Log4j.

Slf4j is a very fast logging wrapper. Usually (e.g. with log4j alone) you would log a debug line with a variable like:

1  
log.debug("speed: " + speed);


Everytime this method gets executed, even if we have set the logging level above debug (and that's the crucial point), a string concatenation takes place. With slf4j you print logging messages this way:

1  
log.debug("speed: {}", speed);


Only if we really want the debug messages logged, we have to perform a string replacement.
Offline noctarius

JGO Knight


Medals: 61


Manager Developer Relations @Hazelcast


« Reply #11 - Posted 2014-09-07 09:44:47 »

You can try chronicles, the fastest way writing logs to disk Smiley https://github.com/peter-lawrey/Java-Chronicle

Pages: [1]
  ignore  |  Print  
 
 

 
EgonOlsen (591 views)
2018-06-10 19:43:48

EgonOlsen (695 views)
2018-06-10 19:43:44

EgonOlsen (478 views)
2018-06-10 19:43:20

DesertCoockie (831 views)
2018-05-13 18:23:11

nelsongames (1026 views)
2018-04-24 18:15:36

nelsongames (1083 views)
2018-04-24 18:14:32

ivj94 (1664 views)
2018-03-24 14:47:39

ivj94 (622 views)
2018-03-24 14:46:31

ivj94 (1501 views)
2018-03-24 14:43:53

Solater (569 views)
2018-03-17 05:04:08
Deployment and Packaging
by philfrei
2018-08-20 02:33:38

Deployment and Packaging
by philfrei
2018-08-20 02:29:55

Deployment and Packaging
by philfrei
2018-08-19 23:56:20

Deployment and Packaging
by philfrei
2018-08-19 23:54:46

Deployment and Packaging
by philfrei
2018-08-19 23:53:08

Deployment and Packaging
by philfrei
2018-08-19 23:50:04

Java Gaming Resources
by philfrei
2017-12-05 19:38:37

Java Gaming Resources
by philfrei
2017-12-05 19:37:39
java-gaming.org is not responsible for the content posted by its members, including references to external websites, and other references that may or may not have a relation with our primarily gaming and game production oriented community. inquiries and complaints can be sent via email to the info‑account of the company managing the website of java‑gaming.org
Powered by MySQL Powered by PHP Powered by SMF 1.1.18 | SMF © 2013, Simple Machines | Managed by Enhanced Four Valid XHTML 1.0! Valid CSS!