禁用以编程方式在控制台上显示日志

Disable showing logs on console programmatically

我想禁用在命令提示符下显示日志,而是将其写入我已实现的日志文件。 我正在使用 java.util.logging.Logger

      public class test{
          static Handler fileHandler = null;
      private static Logger logger = Logger.getLogger(Test.class.getName());
       public static void main(String args[]){

          Path path = Paths.get(PATH,"logfile.log");
         logger.log(Level.INFO, "Using Scanner for Getting Input from User");
         fileHandler = new FileHandler(path.toString(), true);  
          fileHandler.setFormatter(new SimpleFormatter());
           logger.addHandler(fileHandler);

      }
     }

提前致谢

使用此方法记录消息确保正确设置文件路径并使用logger.info("Log message")记录消息:

public static void log(String filepath){
      Logger logger = Logger.getLogger("MyLog");  
      FileHandler fh;
      logger.setUseParentHandlers(false);//Use this to not show output in command line

    try {  

        fh = new FileHandler(filepath);  
        logger.addHandler(fh);
        SimpleFormatter formatter = new SimpleFormatter();  
        fh.setFormatter(formatter);  

        logger.info("My first log");//Use this to log statement

    } catch (SecurityException e) {  
        e.printStackTrace();  
    } catch (IOException e) {  
        e.printStackTrace();  
    }  
}