Class LogsResource

java.lang.Object
jakarta.servlet.GenericServlet
jakarta.servlet.http.HttpServlet
All Implemented Interfaces:
jakarta.servlet.Servlet, jakarta.servlet.ServletConfig, Serializable, BasicUniversalConfig, DefaultConfig, DefaultHtmlConfig

public class LogsResource extends BasicRestServlet
REST resource for viewing and accessing log files.
Routes
RequestDoes
GET /logs/{path}Information on a file, or a directory listing
GET /logs/view/{path}The file's contents as text, or as HTML with ?highlight=true
GET /logs/parse/{path}The file's parsed log entries
GET /logs/download/{path}The file's contents as application/octet-stream
DELETE /logs/{path}Deletes the file or directory, when Logging/allowDeletes is on

The view, parse and download prefixes are reserved: they always name an action, never a directory. A log directory with one of those names cannot be listed through GET /logs/view and so on, but its files are still reachable through the action routes, for example GET /logs/view/view/app.log. The listing's link to such a directory routes to the action and is answered with 404, as is any action on a directory. DELETE reserves no prefix: DELETE /logs/view/app.log deletes app.log in the view directory.

Every read is a plain GET, and the method query parameter is not accepted, so no GET link can delete anything. A delete must be a real DELETE, sent by a REST client or from the command line; the HTML listing has no delete link, because a browser link or form cannot carry the header-borne token a CSRF boundary requires. A DELETE cannot be sent cross-site without a CORS preflight, so a boundary in front of this resource (see AdminBoundary) admits it without a token:

curl -X DELETE http://localhost:10000/logs/old.log

This resource has no authentication of its own. Do not expose it on an untrusted network.

See Also:
See Also:
  • Constructor Details

  • Method Details

    • deleteFile

      public org.apache.juneau.microservice.examples.LogsResource.RedirectToRoot deleteFile(@Path("/*") String path) throws MethodNotAllowed
      Deletes a log file on the file system.
      Parameters:
      path - The file path.
      Returns:
      Redirect to root on success.
      Throws:
      MethodNotAllowed - If deletes are not enabled.
    • downloadFile

      public org.apache.juneau.microservice.examples.LogsResource.FileContents downloadFile(RestResponse res, @Path("/*") String path) throws NotFound
      Downloads the contents of a file.
      Parameters:
      res - The response.
      path - The file path.
      Returns:
      The file contents stream.
      Throws:
      NotFound - If the file is not found, or is a directory.
    • getFile

      Returns information about the specified file or directory.
      Parameters:
      req - The request.
      path - The file path.
      Returns:
      The file or directory details.
      Throws:
      NotFound - If the file is not found.
      Exception - If an error occurs.
    • init

      public void init(Config config)
      Initializes log directory and formatter from config.
      Parameters:
      config - The microservice configuration.
    • viewFile

      public void viewFile(RestResponse res, @Path("/*") String path, @Query(name="highlight",schema=@Schema(d="Add severity color highlighting.")) boolean highlight, @Query(name="start",schema=@Schema(d="Start timestamp (ISO8601, full or partial).\nDon\'t print lines logged before the specified timestamp.\nUse any of the following formats: yyyy, yyyy-MM, yyyy-MM-dd, yyyy-MM-ddThh, yyyy-MM-ddThh:mm, yyyy-MM-ddThh:mm:ss, yyyy-MM-ddThh:mm:ss.SSS")) String start, @Query(name="end",schema=@Schema(d="End timestamp (ISO8601, full or partial).\nDon\'t print lines logged after the specified timestamp.\nUse any of the following formats: yyyy, yyyy-MM, yyyy-MM-dd, yyyy-MM-ddThh, yyyy-MM-ddThh:mm, yyyy-MM-ddThh:mm:ss, yyyy-MM-ddThh:mm:ss.SSS")) String end, @Query(name="thread",schema=@Schema(d="Thread name filter.\nOnly show log entries with the specified thread name.")) String thread, @Query(name="loggers",schema=@Schema(d="Logger filter (simple class name).\nOnly show log entries if they were produced by one of the specified loggers.")) String[] loggers, @Query(name="severity",schema=@Schema(d="Severity filter.\nOnly show log entries with the specified severity.")) String[] severity) throws NotFound, IOException
      Views the contents of a log file with optional filtering.
      Parameters:
      res - The response.
      path - The file path.
      highlight - Add severity color highlighting.
      start - Start timestamp filter.
      end - End timestamp filter.
      thread - Thread name filter.
      loggers - Logger filter.
      severity - Severity filter.
      Throws:
      NotFound - If the file is not found, or is a directory.
      IOException - If an I/O error occurs.
    • viewParsedEntries

      public LogParser viewParsedEntries(RestRequest req, @Path("/*") String path, @Query(name="start",schema=@Schema(d="Start timestamp (ISO8601, full or partial).\nDon\'t print lines logged before the specified timestamp.\nUse any of the following formats: yyyy, yyyy-MM, yyyy-MM-dd, yyyy-MM-ddThh, yyyy-MM-ddThh:mm, yyyy-MM-ddThh:mm:ss, yyyy-MM-ddThh:mm:ss.SSS")) String start, @Query(name="end",schema=@Schema(d="End timestamp (ISO8601, full or partial).\nDon\'t print lines logged after the specified timestamp.\nUse any of the following formats: yyyy, yyyy-MM, yyyy-MM-dd, yyyy-MM-ddThh, yyyy-MM-ddThh:mm, yyyy-MM-ddThh:mm:ss, yyyy-MM-ddThh:mm:ss.SSS")) String end, @Query(name="thread",schema=@Schema(d="Thread name filter.\nOnly show log entries with the specified thread name.")) String thread, @Query(name="loggers",schema=@Schema(d="Logger filter (simple class name).\nOnly show log entries if they were produced by one of the specified loggers.")) String[] loggers, @Query(name="severity",schema=@Schema(d="Severity filter.\nOnly show log entries with the specified severity.")) String[] severity) throws NotFound, IOException
      Views the parsed contents of a log file with filtering.
      Parameters:
      req - The request.
      path - The file path.
      start - Start timestamp filter.
      end - End timestamp filter.
      thread - Thread name filter.
      loggers - Logger filter.
      severity - Severity filter.
      Returns:
      The log parser iterable.
      Throws:
      NotFound - If the file is not found.
      IOException - If an I/O error occurs.