简体   繁体   English

一次将多个实例记录到控制台-java.util.logging.Logger

[英]Logging Multiple Instances Once into Console - java.util.logging.Logger

Getting Java Logger to echo-return a single message inside console. 使Java Logger在控制台内回显单个消息。 After multiple JSON objects are successfully loaded. 成功加载多个JSON对象之后。

In my server/client framework, I have multiple defining JSON objects, server-sided. 在服务器/客户端框架中,我在服务器端有多个定义的JSON对象。 Thus, a java.util.logging.Logger is a helpful informant. 因此, java.util.logging.Logger是有用的通知者。 If or if not, said JSON files or "objects" were created successful, during the initial bootstrap, loading stage. 不管是否,在初始引导加载阶段,是否成功创建了所述JSON文件或“对象”。 Messages echo in-console, detailing that. 消息在控制台中回显,对此进行了详细说明。 Doing exactly as the Logger object should. 完全按照Logger对象的方式进行。

Here, I will give an example from code, reinforcing the need for such a logger running at the levels; 在这里,我将通过代码给出一个示例,以增强这种级别的记录器的运行需求。 Level.INFO & Level.SEVERE . Level.INFOLevel.SEVERE


Code Sample 代码样例

This sample is from a class, DefinitionLoader.java . 该示例来自DefinitionLoader.java类。 Implementing Runnable, as implied by the apparent run() method. 如明显的run()方法所隐含地实现Runnable。

public abstract void load() throws Throwable;
public abstract String file();

@Override
public void run() {
    try {
        long start = System.currentTimeMillis();
        load();
        long elapsed = System.currentTimeMillis() - start;
        Server.getLogger().log(Level.INFO, "Loaded definitions for: "+file()+". It took "+elapsed+" milliseconds.");
    } catch(Throwable e) {
        e.printStackTrace();
        Server.getLogger().log(Level.SEVERE, "Loaded definitions for: "+file(), e);
    }
}

Each singular JSON object, is executed to & from memory via load() / file() calls. 每个单一的JSON对象都通过load() / file()调用从内存中执行。 Necessarily, they are logged. 必要时,将它们记录下来。

There's only one problem with mine & Java 8's logging capabilities. 我的&Java 8的日志记录功能只有一个问题。 Each logging message or indication of a successfully loaded JSON file, is 'spit out' or for better terms, printed to the console. 每条记录消息或成功加载的JSON文件的指示均已“吐出”或以更好的术语显示在控制台上。 Producing an intrusive amount of information, only furthering any hypothetical issues. 产生大量的信息,只会加剧任何假设的问题。 I would love for it to just say, 我很乐意说

All definitions have been loaded successfully. 所有定义均已成功加载。

a single time, within the console, among other bootstrap linguistics. 一次,在控制台内,以及其他引导语言。

Finally, 最后,

What are the implications of just housing a System.out property in the run() method instead of just logging it like this? 仅在run()方法中包含System.out属性而不是像这样记录它的含义是什么? Ideally, logging can do no harm other then when your are bombarded by; 理想情况下,除了受到您的轰炸之外,伐木不会造成任何伤害。

Console Output 控制台输出

INFO: Initializing the Bootstrap...
Feb 23, 2018 10:01:40 PM com.MYPACKAGE.definition.loader.DefinitionLoader run
INFO: Loaded definitions for: ./DIRECTORY/definitions/SOMEJSONFILE.json. It took 344 milliseconds.
Feb 23, 2018 10:01:40 PM com.MYPACKAGE.definition.loader.DefinitionLoader run
INFO: Loaded definitions for: ./DIRECTORY/definitions/SOMEJSONFILE.json. It took 609 milliseconds.
Feb 23, 2018 10:01:40 PM com.MYPACKAGE.definition.loader.DefinitionLoader run
INFO: Loaded definitions for: ./DIRECTORY/definitions/SOMEJSONFILE.json. It took 2 milliseconds.
Feb 23, 2018 10:01:40 PM com.MYPACKAGE.definition.loader.DefinitionLoader run
INFO: Loaded definitions for: ./DIRECTORY/definitions/SOMEJSONFILE.json. It took 32 milliseconds.
Feb 23, 2018 10:01:40 PM com.MYPACKAGE.definition.loader.DefinitionLoader run
INFO: Loaded definitions for: ./DIRECTORY/definitions/SOMEJSONFILE.json. It took 125 milliseconds.
Feb 23, 2018 10:01:40 PM com.MYPACKAGE.definition.loader.DefinitionLoader run
INFO: Loaded definitions for: ./DIRECTORY/definitions/SOMEJSONFILE.json. It took 42 milliseconds.
Feb 23, 2018 10:01:40 PM com.MYPACKAGE.definition.loader.DefinitionLoader run
INFO: Loaded definitions for: ./DIRECTORY/definitions/SOMEJSONFILE.json. It took 47 milliseconds.
Feb 23, 2018 10:01:41 PM com.MYPACKAGE.Server main
INFO: The Bootstrap has been bound, SERVER is now online!

In conclusion, 结论,

I made an attempt at processing the logger through a for loop . 我尝试通过for loop处理记录器。 Also, I passed a bool parameter using the constructor: log(Level level, String msg, Object[] params) . 另外,我使用构造函数传递了bool参数: log(Level level, String msg, Object[] params) Finally, to my surprise after taking to the web for answers in dealing with my situation. 最后,在上网查询处理我的情况后,令我惊讶的是。 Any and only clues I could find, provided nothing in relation to the logging spam. 我所能找到的任何唯一线索,都与垃圾邮件记录无关。 Just want to say, thank you to the stranger(s) &/or whoever can & is willing to help! 只是想说,谢谢陌生人和/或愿意并且愿意帮助的人!

A possible solution could be a countdown latch. 可能的解决方案可能是倒计时闩锁。 This basically means that each thread is given a mechanism to signal that it has done its job, and the calling thread can wait for all threads to finish. 基本上,这意味着每个线程都具有一种机制来发出信号,表明其已完成其工作,并且调用线程可以等待所有线程完成。

Here's the example from Java's implementation 这是Java实现的示例

 class Driver2 {
   void main() throws InterruptedException {
     CountDownLatch doneSignal = new CountDownLatch(N);
     Executor e = ...

     for (int i = 0; i < N; ++i) // create and start threads
       e.execute(new WorkerRunnable(doneSignal, i));

     doneSignal.await();           // wait for all to finish
   }
 }

 class WorkerRunnable implements Runnable {
   private final CountDownLatch doneSignal;
   WorkerRunnable(CountDownLatch doneSignal) {
      this.doneSignal = doneSignal;
   }
   public void run() {
      try {
        doWork();
        doneSignal.countDown();
      } catch (InterruptedException ex) {} // return;
   }
 }

You could add a log message once the await has finished, and also check that each thread finished successfully (by having them write a result somewhere, or by inspecting a public property on them directly). 您可以在await结束后添加一条日志消息,还可以检查每个线程是否成功完成(通过让它们在某个地方写入结果,或者直接检查它们的公共属性)。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM