grautur
grautur

Reputation: 30505

best practices for writing to a file from multiple methods

I have a class that contains a bunch of methods for checking data I scrape every week (for things like well-formedness and other errors in gathering the data). Each of these methods performs a test, and then prints out a summary of the test.

I want to print out the output from these tests to a file, but I'm not sure what the best way to do it is. For example...

I'm not really familiar with using logger libraries -- would that be a solution?

My particular context

I have a scraper that pulls data from various websites and stores them in a database. Websites change all the time, so I'm writing a "scrape checker" program that checks my scrapes for various things, like:

So I have methods like:

I want my "scrape checker" program to print out a file that summarizes all the checks.

Upvotes: 0

Views: 62

Answers (3)

orangepips
orangepips

Reputation: 9971

Separation of concerns. Write code the focuses on the scraping activity and return the value(s) scraped. Then use aspect oriented programming for logging, which can simplify the problem greatly as the aspect holds the reference to the file or logging API.

Upvotes: 2

Eric Petroelje
Eric Petroelje

Reputation: 60529

Seems like a logging framework would be a perfect solution for this. If you are using Java or .NET, log4j and log4net are pretty much the de-facto standards for that.

Upvotes: 1

Rafe Kettler
Rafe Kettler

Reputation: 76965

Ultimately, it depends on what language you're using.

The first solution makes the most sense if your language permits it. For each instance of the logging class, have a field for the file object that you're reading from/writing to. This is basically equivalent to passing the file object as a parameter to every method.

That said, most mature languages have modules that will do a lot of this work for you; off the top of my sh/awk, Perl, and Python all come to mind as being suited to this task (though if you want to, you could use Java or something else).

Upvotes: 1

Related Questions