DZone
Thanks for visiting DZone today,
Edit Profile
  • Manage Email Subscriptions
  • How to Post to DZone
  • Article Submission Guidelines
Sign Out View Profile
  • Post an Article
  • Manage My Drafts
Over 2 million developers have joined DZone.
Log In / Join
Refcards Trend Reports
Events Video Library
Refcards
Trend Reports

Events

View Events Video Library

Related

  • Import a Function/Module in Dataweave
  • Mitigating Cache Stampedes in Dynamic API Translation Using Java 21 Virtual Threads
  • AGENTS.md Makes Your Java Codebase AI-Agent Ready
  • Going Stateless: Scaling MCP Servers to Cloud-Native Java and HTTP

Trending

  • Agents, Tools, and MCP: A Mental Model That Actually Helps
  • 12 Factor Framework for Building Secure and Compliant Cloud Applications
  • Does 100% Code Coverage Mean Tested?
  • From Gherkin to Source Code Without Losing the Business Language
  1. DZone
  2. Coding
  3. Java
  4. Log Scraping

Log Scraping

By 
Tim Spann user avatar
Tim Spann
DZone Core CORE ·
Jun. 05, 13 · Interview
Likes (1)
Comment
Save
Tweet
Share
9.3K Views

Join the DZone community and get the full member experience.

Join For Free

A quick Java snippet for log scraping:

package com.agilemobiledeveloper.logcheck;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;

import com.jcraft.jsch.Channel;
import com.jcraft.jsch.ChannelSftp;
import com.jcraft.jsch.JSch;
import com.jcraft.jsch.Session;

/**
*
* @author spannt
*
*/
public class LogScraper {

/**
* @param args
*/
public static void main(String[] args) {
String SFTPHOST = "myunixsite.com";
int SFTPPORT = 22;
String SFTPUSER = "myunixid";
String SFTPPASS = "myunixpassword";
String SFTPWORKINGDIR = "/some/unix/directory";
String SERRORFILE = "SystemErr.log";
String SOUTFILE = "SystemOut.log";

Session session = null;
Channel channel = null;
ChannelSftp channelSftp = null;

StringBuilder out = new StringBuilder();

try {
JSch jsch = new JSch();
session = jsch.getSession(SFTPUSER, SFTPHOST, SFTPPORT);
session.setPassword(SFTPPASS);
java.util.Properties config = new java.util.Properties();
config.put("StrictHostKeyChecking", "no");
session.setConfig(config);
session.connect();
channel = session.openChannel("sftp");
channel.connect();
channelSftp = (ChannelSftp) channel;
channelSftp.cd(SFTPWORKINGDIR);

System.out.println("Error File");
out.append("Error File:").append(
LogScraper.parseStream(channelSftp.get(SERRORFILE)));

System.out.println("Output File");
out.append("Output File:").append(
LogScraper.parseStream(channelSftp.get(SOUTFILE)));

} catch (Exception ex) {
ex.printStackTrace();
out.append(ex.getLocalizedMessage());
}

System.out.println("Logs=" + out.toString());

}

/**
*
* line.contains("Exception") ||
*
* @param file
* @return String of error data
*/
public static String parseStream(InputStream inputFileStream) {
if ( null == inputFileStream ) { return "Log Empty"; }
StringBuilder out = new StringBuilder();

BufferedReader br = new BufferedReader(new InputStreamReader(
inputFileStream));

String line = null;
try {
while ((line = br.readLine()) != null) {
if (line.contains("OutOfMemoryError")) {
out.append(line).append(System.lineSeparator());
}
}
} catch (IOException e) {
e.printStackTrace();
out.append(e.getLocalizedMessage());
}

return out.toString();
}
}
Snippet (programming) Java (programming language)

Published at DZone with permission of Tim Spann. See the original article here.

Opinions expressed by DZone contributors are their own.

Related

  • Import a Function/Module in Dataweave
  • Mitigating Cache Stampedes in Dynamic API Translation Using Java 21 Virtual Threads
  • AGENTS.md Makes Your Java Codebase AI-Agent Ready
  • Going Stateless: Scaling MCP Servers to Cloud-Native Java and HTTP

Partner Resources

×

Comments

The likes didn't load as expected. Please refresh the page and try again.

  • RSS
  • X
  • Facebook

ABOUT US

  • About DZone
  • Support and feedback
  • Community research

ADVERTISE

  • Advertise with DZone

CONTRIBUTE ON DZONE

  • Article Submission Guidelines
  • Become a Contributor
  • Core Program
  • Visit the Writers' Zone

LEGAL

  • Terms of Service
  • Privacy Policy

CONTACT US

  • 3343 Perimeter Hill Drive
  • Suite 215
  • Nashville, TN 37211
  • [email protected]

Let's be friends:

  • RSS
  • X
  • Facebook