问题
I want to have the output of logger and inputstream
in the eclipse console. But everytime I execute, the result is always not in the same order. I have several classes with one main class to call the others and I put logger to each method for debugging. I print the result to console. I also have the method to retrieve the inputstream
as string
and print it into console.
The output example
mai 19, 2015 4:10:58 PM ScriptPack.Section findSection
INFO: findSection OK
mai 19, 2015 4:10:58 PM ScriptPack.Section convertStringToTestCase
INFO: convertStringToTestCase OK
mai 19, 2015 4:10:58 PM ScriptPack.TestCase getIsPassed
INFO: getIsPassed OK
mai 19, 2015 4:10:58 PM ScriptPack.TestCase getName
INFO: getName OK
//Error.
//**********EasyTest Test Report**********
//Test Case: testCase/test1.tc
//Result : FAILED
//Start at: 2015-05-19-T16:10:58.479
mai 19, 2015 4:10:58 PM ScriptPack.TestCase settIterationNumber
INFO: settIterationNumber OK
mai 19, 2015 4:10:58 PM ScriptPack.TestCase getIterationNumber
INFO: getIterationNumber OK
mai 19, 2015 4:10:58 PM ScriptPack.TestCase getIsPassed
INFO: getIsPassed OK
Iteration number : 1
mai 19, 2015 4:10:58 PM ScriptPack.TestCase getName
INFO: getName OK
//**********EasyTest Test Report**********
//Test Case: testCase/test2.tc
mai 19, 2015 4:10:59 PM ScriptPack.TestCase setIsPassed
INFO: setIsPassed OK
//Result : PASSED
//Start at: 2015-05-19-T16:10:58.838
Iteration number : 1
mai 19, 2015 4:10:59 PM ScriptPack.TestCase settIterationNumber
INFO: settIterationNumber OK
mai 19, 2015 4:10:59 PM ScriptPack.TestCase getIterationNumber
INFO: getIterationNumber OK
mai 19, 2015 4:10:59 PM ScriptPack.TestCase getIsPassed
INFO: getIsPassed OK
mai 19, 2015 4:10:59 PM ScriptPack.TestCase getName
INFO: getName OK
//**********EasyTest Test Report**********
//Test Case: testCase/test3.tc
mai 19, 2015 4:10:59 PM ScriptPack.TestCase setIsPassed
INFO: setIsPassed OK
//Result : PASSED
//Start at: 2015-05-19-T16:10:59.229
mai 19, 2015 4:10:59 PM ScriptPack.TestCase settIterationNumber
INFO: settIterationNumber OK
Iteration number : 1
mai 19, 2015 4:10:59 PM ScriptPack.TestCase getIterationNumber
INFO: getIterationNumber OK
mai 19, 2015 4:10:59 PM ScriptPack.TestCase getIsPassed
INFO: getIsPassed OK
mai 19, 2015 4:10:59 PM ScriptPack.TestCase getIsPassed
INFO: getIsPassed OK
mai 19, 2015 4:10:59 PM ScriptPack.TestCase getIsPassed
INFO: getIsPassed OK
mai 19, 2015 4:10:59 PM ScriptPack.TestCaseList verifyList
INFO: verifyList NOK
mai 19, 2015 4:10:59 PM ScriptPack.TestCase getIsPassed
INFO: getIsPassed OK
mai 19, 2015 4:10:59 PM ScriptPack.TestCase getName
INFO: getName OK
mai 19, 2015 4:10:59 PM ScriptPack.TestCase settIterationNumber
INFO: settIterationNumber OK
mai 19, 2015 4:10:59 PM ScriptPack.TestCase getIterationNumber
INFO: getIterationNumber OK
mai 19, 2015 4:10:59 PM ScriptPack.TestCase getIsPassed
INFO: getIsPassed OK
mai 19, 2015 4:10:59 PM ScriptPack.TestCase getIsPassed
INFO: getIsPassed OK
mai 19, 2015 4:10:59 PM ScriptPack.TestCase getIsPassed
INFO: getIsPassed OK
mai 19, 2015 4:10:59 PM ScriptPack.TestCase getIsPassed
INFO: getIsPassed OK
mai 19, 2015 4:10:59 PM ScriptPack.TestCase getIsPassed
INFO: getIsPassed OK
mai 19, 2015 4:10:59 PM ScriptPack.TestCaseList verifyList
INFO: verifyList NOK
//Error.
//**********EasyTest Test Report**********
//Test Case: testCase/test1.tc
//Result : FAILED
//Start at: 2015-05-19-T16:10:59.635
Iteration number : 2
Then I re-execute and get different order
mai 19, 2015 4:13:24 PM ScriptPack.Section findSection
INFO: findSection OK
mai 19, 2015 4:13:24 PM ScriptPack.Section convertStringToTestCase
INFO: convertStringToTestCase OK
mai 19, 2015 4:13:24 PM ScriptPack.TestCase getIsPassed
INFO: getIsPassed OK
mai 19, 2015 4:13:24 PM ScriptPack.TestCase getName
INFO: getName OK
//Error.
//**********EasyTest Test Report**********
//Test Case: testCase/test1.tc
//Result : FAILED
//Start at: 2015-05-19-T16:13:24.321
mai 19, 2015 4:13:24 PM ScriptPack.TestCase settIterationNumber
INFO: settIterationNumber OK
mai 19, 2015 4:13:24 PM ScriptPack.TestCase getIterationNumber
INFO: getIterationNumber OK
Iteration number : 1
mai 19, 2015 4:13:24 PM ScriptPack.TestCase getIsPassed
INFO: getIsPassed OK
mai 19, 2015 4:13:24 PM ScriptPack.TestCase getName
INFO: getName OK
//**********EasyTest Test Report**********
//Test Case: testCase/test2.tc
mai 19, 2015 4:13:25 PM ScriptPack.TestCase setIsPassed
INFO: setIsPassed OK
mai 19, 2015 4:13:25 PM ScriptPack.TestCase settIterationNumber
INFO: settIterationNumber OK
mai 19, 2015 4:13:25 PM ScriptPack.TestCase getIterationNumber
INFO: getIterationNumber OK
mai 19, 2015 4:13:25 PM ScriptPack.TestCase getIsPassed
INFO: getIsPassed OK
mai 19, 2015 4:13:25 PM ScriptPack.TestCase getName
INFO: getName OK
//Result : PASSED
//Start at: 2015-05-19-T16:13:24.789
Iteration number : 1
//**********EasyTest Test Report**********
//Test Case: testCase/test3.tc
//Result : PASSED
//Start at: 2015-05-19-T16:13:25.228
mai 19, 2015 4:13:25 PM ScriptPack.TestCase setIsPassed
INFO: setIsPassed OK
mai 19, 2015 4:13:25 PM ScriptPack.TestCase settIterationNumber
INFO: settIterationNumber OK
Iteration number : 1
mai 19, 2015 4:13:25 PM ScriptPack.TestCase getIterationNumber
INFO: getIterationNumber OK
mai 19, 2015 4:13:25 PM ScriptPack.TestCase getIsPassed
INFO: getIsPassed OK
mai 19, 2015 4:13:25 PM ScriptPack.TestCase getIsPassed
INFO: getIsPassed OK
mai 19, 2015 4:13:25 PM ScriptPack.TestCase getIsPassed
INFO: getIsPassed OK
mai 19, 2015 4:13:25 PM ScriptPack.TestCaseList verifyList
INFO: verifyList NOK
mai 19, 2015 4:13:25 PM ScriptPack.TestCase getIsPassed
INFO: getIsPassed OK
mai 19, 2015 4:13:25 PM ScriptPack.TestCase getName
INFO: getName OK
mai 19, 2015 4:13:25 PM ScriptPack.TestCase settIterationNumber
INFO: settIterationNumber OK
mai 19, 2015 4:13:25 PM ScriptPack.TestCase getIterationNumber
INFO: getIterationNumber OK
mai 19, 2015 4:13:25 PM ScriptPack.TestCase getIsPassed
INFO: getIsPassed OK
mai 19, 2015 4:13:25 PM ScriptPack.TestCase getIsPassed
INFO: getIsPassed OK
mai 19, 2015 4:13:25 PM ScriptPack.TestCase getIsPassed
INFO: getIsPassed OK
mai 19, 2015 4:13:25 PM ScriptPack.TestCase getIsPassed
INFO: getIsPassed OK
mai 19, 2015 4:13:25 PM ScriptPack.TestCase getIsPassed
INFO: getIsPassed OK
//Error.
//**********EasyTest Test Report**********
//Test Case: testCase/test1.tc
//Result : FAILED
//Start at: 2015-05-19-T16:13:25.587
Iteration number : 2
mai 19, 2015 4:13:25 PM ScriptPack.TestCaseList verifyList
INFO: verifyList NOK
The line starts with double slash comes from inputstream
and should be assembled. I tried to put synchronized in each method but doesn't work. If I put Thread.sleep()
, where should I put it? Is there any more safety way to synchronize the output?
回答1:
The issue has been resolved through a discussion in the comments to the question. I'm posting this information as an answer in case someone else has the same problem and finds this question when searching.
Logger API's usually outputs logging to stderr
if no specific configuration has been done. Calls to System.out.println()
will of course go to stdout
.
The problem with output to a console differing at each run of a program, when intermixing both methods, is due to the fact that stdout
is usually buffered, while stderr
is not. To fix this kind of problem, one has to make sure that both methods prints to the same type of outputstream, both to stdout
or both to stderr
.
To find out how to do this for the logger API, you have to look in the documentation for the logger. For System.out.println()
(and the other print methods) you will have to switch to System.err
instead, i.e. System.err.println()
, or vise versa.
回答2:
instead of sleep, you can use wait if you know the amount of time you want each thread to wait. Or use one thread to lock and open the other... it all depends on your needs. When using multi thread the result will never be exactly the same unless they are NOT working simultaneously. If the order is impotent you have some changes that need to be done. Try using one of the thread pool family objects. Here a tutorial that might help http://www.tutorialspoint.com/java/java_multithreading.htm
来源:https://stackoverflow.com/questions/30329258/output-of-logger-and-system-out-println-are-not-in-order