Skip to content
This repository has been archived by the owner on Sep 2, 2021. It is now read-only.

Commit

Permalink
- using winston for logging
Browse files Browse the repository at this point in the history
  • Loading branch information
nilakanta committed Jan 13, 2012
1 parent 3e17e4d commit 5661f1a
Show file tree
Hide file tree
Showing 7 changed files with 39 additions and 28 deletions.
9 changes: 8 additions & 1 deletion conf.coffee
@@ -1,6 +1,6 @@
cradle = require 'sreeix-cradle'
redis = require 'redis'

# winston= require 'winston'
Conf = exports = module.exports

process.env.NODE_ENV ||= "development"
Expand Down Expand Up @@ -36,3 +36,10 @@ Conf.npmDb = new cradle.Connection(Conf.couchdb.npm_registry.host, Conf.couchdb.

Conf.redisClient = redis.createClient Conf.redis.port, Conf.redis.host
Conf.redisClient.auth Conf.redis.auth

# Conf.logger = new (winston.Logger)({
# transports: [
# new (winston.transports.Console)(),
# new (winston.transports.File)({ filename: '.log' })
# ]
# });
8 changes: 5 additions & 3 deletions lib/extensions.coffee
@@ -1,10 +1,12 @@
winston = require 'winston'

exports.createIfNotExisting = (db) ->
db.exists (err, exists) ->
if err
console.log "createIfNotExisting:error", err
winston.error "createIfNotExisting:error", err
else if exists
console.log "createIfNotExisting:database - #{db.name} already exists!"
winston.debug "createIfNotExisting:database - #{db.name} already exists!"
else
console.log "createIfNotExisting:database - #{db.name} does not exist. Creating."
winston.debug "createIfNotExisting:database - #{db.name} does not exist. Creating."
do db.create

6 changes: 4 additions & 2 deletions lib/helper.coffee
@@ -1,6 +1,8 @@
winston = require 'winston'

exports.print = (module = "") ->
(err, info) ->
if err
console.log "Error: #{module} : #{err}"
winston.error "Error: #{module} : #{err}"
else
console.log "Complete: #{module} : #{info}"
winston.info "Complete: #{module} : #{info}"
26 changes: 13 additions & 13 deletions models/package.coffee
Expand Up @@ -134,37 +134,37 @@ exports.watch_updates = () ->
Conf.redisClient.set 'current_npm_id', value, helper.print
else
redisPosition = value
winston.log "setting redis current_npm_id to #{redisPosition}"
winston.info "setting redis current_npm_id to #{redisPosition}"
Conf.packageDatabase.changes(since: parseInt(redisPosition, 10) , feed: 'continuous').on 'response', (res) ->
res.on 'data', (change) ->
winston.log "New change on #{util.inspect(change)}"
winston.info "New change on #{util.inspect(change)}"
Conf.packageDatabase.get change.id, (err, doc) ->
Conf.redisClient.incr 'current_npm_id', helper.print
if not err and doc?.keywords
winston.log "updating changes for keywords #{doc.keywords}"
winston.info "updating changes for keywords #{doc.keywords}"
exports.updateChanged doc
else
winston.log "Error in getting document for #{change._id} #{util.inspect(err)}" if err
winston.error "Error in getting document for #{change._id} #{util.inspect(err)}" if err

exports.updateChanged = (doc, keywords=undefined) ->
try
keywords = keywords || doc.keywords
keywords = if _.isArray(keywords) then keywords else [keywords]
categories = _.map keywords, (keyword) ->
CategoryMap.from_keyword keyword
exports.save_categories doc.id, categories, (err, doc) -> winston.log "updateChanged:docid-> #{doc._id}"
exports.save_categories doc.id, categories, (err, doc) -> winston.info "updateChanged:docid-> #{doc._id}"
catch error
winston.log "updateChanged:Error #{error}"
winston.error "updateChanged:Error #{error}"
if doc.repository?.url
winston.log "Repo url -> #{doc.repository.url}"
winston.info "Repo url -> #{doc.repository.url}"
regex = /github.com\/(.*)\/(.*)\.git/
match = doc.repository.url.match regex
if match and match[1] and match[2]
PackageMetadata.createOrUpdate id: doc.id, user: match[1], repo: match[2], (err, res) ->
if err
winston.log "createOrUpdateError:error : #{err}"
winston.error "createOrUpdateError:error : #{err}"
else
winston.log "createOrUpdateError:response : #{res}"
winston.info "createOrUpdateError:response : #{res}"

exports.import_from_npm = (o, callback) ->
couchConfig = Conf.couchdb
Expand Down Expand Up @@ -198,7 +198,7 @@ exports.save_categories = (name, category_name, callback) ->
categories = _.flatten [category_name]
Conf.metadataDatabase.get name, (err, metaDoc) ->
if(err)
winston.log "creating new doc for #{name}"
winston.info "creating new doc for #{name}"
Conf.metadataDatabase.save name, categories: categories
else
if metaDoc['categories']?
Expand All @@ -208,9 +208,9 @@ exports.save_categories = (name, category_name, callback) ->
metaDoc['categories'] = exports.capitaliseCategories(metaDoc['categories'])
Conf.metadataDatabase.save name, metaDoc['_rev'], metaDoc, (err, res) ->
if err
winston.log "save_categories: error:#{name} #{err}"
winston.error "save_categories: error:#{name} #{err}"
else
winston.log "Successfuly saved #{name} : #{util.inspect(metaDoc['categories'])}"
winston.info "Successfuly saved #{name} : #{util.inspect(metaDoc['categories'])}"

exports.by_rank = (number_of_items = 10, callback) ->
Conf.metadataDatabase.view 'categories/rank', {limit: number_of_items, descending: true}, (err, docs) ->
Expand Down Expand Up @@ -244,7 +244,7 @@ exports.by_category = (category_name, top_count = 10, callback) ->
callback.apply null, [results]

exports.find = (name, callback) ->
winston.log " finding package #{name}"
winston.info " finding package #{name}"
Conf.metadataDatabase.get name, (err, doc) ->
if err or not doc
callback err, null
Expand Down
8 changes: 4 additions & 4 deletions models/package_metadata.coffee
Expand Up @@ -50,18 +50,18 @@ PackageMetadata.createOrUpdate = (opts, callback) ->
callback = (err, res)-> winston.log(err || res)
github.getRepoApi().show opts.user, opts.repo, (err, github_info) ->
if err or !github_info
winston.log "error during fetch of the github info for #{opts.repo}, #{util.inspect(err)}"
winston.error "error during fetch of the github info for #{opts.repo}, #{util.inspect(err)}"
callback.apply null, [err, null]
else
winston.log "Saving new document for #{opts.id}"
winston.info "Saving new document for #{opts.id}"
Conf.metadataDatabase.get opts.id , (err, doc) ->
if doc
data = {}
_.extend data, doc, github: github_info
Conf.metadataDatabase.save opts.id, doc['_rev'], data, (err, res) ->
winston.log "update doc #{res}"
winston.info "update doc #{res}"
callback.apply null, [err, res]
else
Conf.metadataDatabase.save opts.id, github: github_info, (err, res) ->
winston.log "new doc #{res}"
winston.info "new doc #{res}"
callback.apply null, [err, res]
4 changes: 2 additions & 2 deletions models/user.coffee
Expand Up @@ -10,13 +10,13 @@ Conf.userDatabase.get '_design/docs', (err, res) ->
by_github:
map: "function(doc) { if (doc.githubId) emit(doc.githubId, doc); }"
, (err, res) ->
console.log "error in creating views for user #{err.reason}" if err
winston.error "error in creating views for user #{err.reason}" if err
)

exports.findOrCreate = (source, user_id, user_name, accessToken, accessTokenSecret, promise) ->
Conf.userDatabase.view "docs/by_#{source}", key: user_id, (err, docs) ->
if err
console.log "Error using users/_design/docs/_view/by_#{source} #{err.reason}"
winston.error "Error using users/_design/docs/_view/by_#{source} #{err.reason}"
promise.fail err
return
if docs.length > 0
Expand Down
6 changes: 3 additions & 3 deletions server.coffee
Expand Up @@ -108,15 +108,15 @@ app.post '/packages/:name/like', (req, res, next) ->
port = process.env.PORT || 4000

app.listen port, () ->
winston.log "app started at port #{port}"
winston.info "app started at port #{port}"

do packages.watch_updates

if process.env.ENV_VARIABLE is 'production'
new cron.CronJob '0 0 4 * * * *', () ->
winston.log "Running github sync Cron now"
winston.info "Running github sync Cron now"
packages.import_from_github {}, helper.print("github sync")

new cron.CronJob '0 0 5 * * * *', () ->
winston.log "Running Import job Cron now"
winston.info "Running Import job Cron now"
packages.import_from_npm {}, helper.print( "NPM Import")

0 comments on commit 5661f1a

Please sign in to comment.