Esempio n. 1
0
  /**
   * We're here because for some reason, we can't write to the logging database. So instead, we're
   * just going to write directly to a file. The filename will be the name of the service + ".log"
   * and in the current directory.
   *
   * @param level int severity level
   * @param serviceName String name of the service making the call
   * @param methodClass String class of the caller
   * @param method String method name of the caller
   * @param message String message to be logged
   */
  private static synchronized void handleNonLoggableError(
      LogLevel level, String serviceName, Class methodClass, String method, String message) {
    final String DATE_FORMAT = "yyyy/MM/dd HH:mm:ss.SSS";

    FileOutputStream stream = null;
    try {
      String logFilename = getLogFilename(serviceName);
      StringBuffer buf = new StringBuffer();

      buf.append(new SimpleDateFormat(DATE_FORMAT).format(Gmt.getGmtTimestamp()));
      buf.append(" ");
      buf.append(level);
      if (methodClass != null) {
        buf.append(" ");
        String className = methodClass.getName();
        int idx = className.lastIndexOf('.');

        buf.append(idx == -1 ? className : className.substring(idx + 1));
        buf.append(".");
        buf.append(method);
      }

      buf.append(" ");
      buf.append(message);
      buf.append(System.getProperty("line.separator"));

      stream = new FileOutputStream(logFilename, true);

      // Because this is a synchronized method, and no other app instance should be writing to this
      // file, we
      // should have excluse access to the file. So we don't need any file locking.
      String msg = buf.toString();
      stream.write(msg.getBytes());
      //noinspection UseOfSystemOutOrSystemErr
      System.out.println(msg); // this is the fallback for when the 'more-robust-logging' fails
    } catch (Exception e) {
      // Give up on doing anything with an exception here. There's simply nothing more we can do
      // to attempt to log something.
    } finally {
      if (stream != null) {
        //noinspection EmptyCatchBlock
        try {
          stream.close();
        } catch (Throwable t) {
        }
      }
    }
  }