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

Zones

Culture and Methodologies Agile Career Development Methodologies Team Management
Data Engineering AI/ML Big Data Data Databases IoT
Software Design and Architecture Cloud Architecture Containers Integration Microservices Performance Security
Coding Frameworks Java JavaScript Languages Tools
Testing, Deployment, and Maintenance Deployment DevOps and CI/CD Maintenance Monitoring and Observability Testing, Tools, and Frameworks
Culture and Methodologies
Agile Career Development Methodologies Team Management
Data Engineering
AI/ML Big Data Data Databases IoT
Software Design and Architecture
Cloud Architecture Containers Integration Microservices Performance Security
Coding
Frameworks Java JavaScript Languages Tools
Testing, Deployment, and Maintenance
Deployment DevOps and CI/CD Maintenance Monitoring and Observability Testing, Tools, and Frameworks

Generative AI has transformed nearly every industry. How can you leverage GenAI to improve your productivity and efficiency?

SBOMs are essential to circumventing software supply chain attacks, and they provide visibility into various software components.

Related

  • Import a Function/Module in Dataweave
  • Dynamically Evaluate Dataweave Scripts
  • Modernizing Apache Spark Applications With GenAI: Migrating From Java to Scala
  • Squid Game: The Clean Code Trials — A Java Developer's Survival Story

Trending

  • The QA Paradox: To Save Artificial Intelligence, We Must Stop Blindly Trusting Data—And Start Trusting Human Judgment
  • How My AI Agents Learned to Talk to Each Other With A2A
  • Advanced Insight Generation: Revolutionizing Data Ingestion for AI-Powered Search
  • Software Specs 2.0: An Elaborate Example
  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
8.9K 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, DZone MVB. See the original article here.

Opinions expressed by DZone contributors are their own.

Related

  • Import a Function/Module in Dataweave
  • Dynamically Evaluate Dataweave Scripts
  • Modernizing Apache Spark Applications With GenAI: Migrating From Java to Scala
  • Squid Game: The Clean Code Trials — A Java Developer's Survival Story

Partner Resources

×

Comments

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

ABOUT US

  • About DZone
  • Support and feedback
  • Community research
  • Sitemap

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 100
  • Nashville, TN 37211
  • [email protected]

Let's be friends: