<commit_before>// Copyright 2014 logentries.com
var net=require('net'), tls=require('tls'), evt=require('events'), util=require('util');

function Logger(ops){
  
  var self=this, badLevelNames=['log','end','level','levels','on','once'];
  var options = {
    host: ops.host || 'api.logentries.com',
    port: ops.secure ? 20000 : 10000,
    token: <PASSWORD>.token
  }
  var sok = ops.secure ? tls.connect(options.port, options.host) : net.createConnection(options.port, options.host);

  self.levels = ops.levels || { debug:0, info:1, notice:2, warning:3, err:4, crit:5, alert:6, emerg:7 };
  self.minLevel = 1;

  // Create a named function for each level (dumb convenience!)
  for(var lx in self.levels) if(badLevelNames.indexOf(lx)<0) self[lx] = function(l){
    return function(){
      var args = Array.prototype.slice.call(arguments);
      args.unshift(l);
      self.log.apply(self,args);
    }
  }(lx);
 
  // Format and output log message
  self.log = function(){
    var a=arguments, line=ops.token;
    if(a[0]===undefined || self.levels[a[0]]===undefined) self.emit('error','unknown log level: '+a[0]);
    else if(self.levels[a[0]] >= self.minLevel){
      for(var i in a) line+=' '+(typeof(a[i])=='object' ? JSON.stringify(a[i]) : a[i]);
      line.replace(/\n/g, '\u2028');
      //console.log(line);
      try{ 
        sok.write(line+'\n');
        self.emit('log',line);
      }catch(e){ self.emit('error',e) }
    }
  };

  // Set min level (by name), below which log messages wil be discarded
  self.level = function(lmin){
    if(self.levels[lmin]>=0){
      self.minLevel = self.levels[lmin];
      if(self.winstonLogger) self.winstonLogger.level = lmin;
    }
  }

  // Register leLogger as transport with winston
  self.winston = function(winston,wops){
    self.levels = (wops && wops.levels) || winston.levels;
    if(!winston.transports.LogentriesLogger){
      var LogentriesLogger = winston.transports.LogentriesLogger = function(op){
        op = op || {};
        this.name = 'logentries';
        this.level = op.level || 'info';
        if(op.level) self.level(op.level);
        self.winstonLogger = this;
      }
      util.inherits(LogentriesLogger, winston.Transport);
      LogentriesLogger.prototype.log = function(level,msg,meta,callback){
        var data = msg + (meta ? ' '+JSON.stringify(meta) : '');
        self.log(level,data);
        callback(null, true); 
      }
    }
    winston.add(LogentriesLogger,wops);
  }
  
}

util.inherits(Logger, evt.EventEmitter);
exports.logger = function(o){ return new Logger(o) }<commit_msg>timestamp added. end()?<commit_after>// Copyright (c) 2014 logentries.com
var net=require('net'), tls=require('tls'), evt=require('events'), util=require('util');

function Logger(ops){
  
  var self = this; 
  var options = {
    host: ops.host || 'api.logentries.com',
    port: ops.secure ? 20000 : 10000,
    token: ops.token,
    addTS: ops.timestamp!==undefined ? ops.timestamp : true
  }
  
  var sok = ops.secure ? tls.connect(options.port, options.host) : net.createConnection(options.port, options.host);
  var badLevelNames = ['log','end','level','levels','on','once'];

  self.levels = ops.levels || { debug:0, info:1, notice:2, warning:3, err:4, crit:5, alert:6, emerg:7 };
  self.minLevel = 1;

  // Create a named function for each level (dumb convenience!)
  for(var lx in self.levels) if(badLevelNames.indexOf(lx)<0) self[lx] = function(l){
    return function(){
      var args = Array.prototype.slice.call(arguments);
      args.unshift(l);
      self.log.apply(self,args);
    }
  }(lx);
 
  // Format and output log message
  self.log = function(){
    var a=arguments, line=ops.token;
    if(options.addTS) line+=' '+new Date().toISOString();
    if(a[0]===undefined || self.levels[a[0]]===undefined) self.emit('error','unknown log level: '+a[0]);
    else if(self.levels[a[0]] >= self.minLevel){
      for(var i in a) line+=' '+(typeof(a[i])=='object' ? JSON.stringify(a[i]) : a[i]);
      line.replace(/\n/g, '\u2028');
      //console.log(line);
      try{ 
        sok.write(line+'\n');
        self.emit('log',line);
      }catch(e){ self.emit('error',e) }
    }
  };

  // Set min level (by name), below which log messages wil be discarded
  self.level = function(lmin){
    if(self.levels[lmin]>=0){
      self.minLevel = self.levels[lmin];
      if(self.winstonLogger) self.winstonLogger.level = lmin;
    }
  };
  
  self.end = function(){ // Why?
//    if(sok) try{
//      sok.end();
//    }catch(e){ self.emit('error',e); }
    self.emit('end');
  }


  // Register leLogger as transport with winston
  self.winston = function(winston,wops){
    self.levels = (wops && wops.levels) || winston.levels;
    if(!winston.transports.LogentriesLogger){
      var LogentriesLogger = winston.transports.LogentriesLogger = function(op){
        op = op || {};
        this.name = 'logentries';
        this.level = op.level || 'info';
        if(op.level) self.level(op.level);
        self.winstonLogger = this;
      }
      util.inherits(LogentriesLogger, winston.Transport);
      LogentriesLogger.prototype.log = function(level,msg,meta,callback){
        var data = msg + (meta ? ' '+JSON.stringify(meta) : '');
        self.log(level,data);
        callback(null, true); 
      }
    }
    winston.add(LogentriesLogger,wops);
  }
  
}

util.inherits(Logger, evt.EventEmitter);
exports.logger = function(o){ return new Logger(o) }

// Do we need to support multiple logger instances (with different tokens) within single Node app..?
{'fraction_non_alphanumeric': 0.17596101786681104, 'fraction_numerical': 0.011911207363291824, 'mean_word_length': 3.243491577335375, 'pattern_counts': {'":': 0, '<': 6, '<?xml version=': 0, '>': 8, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '461934', 'n_tokens_mistral': 1956, 'n_tokens_neox': 1854, 'n_words': 380}