Permalink
Browse files

Attach logging library to GLOBAL.

Add Idea project.
  • Loading branch information...
vjpr committed May 12, 2013
1 parent 6ae1b80 commit bcb2c3732ea8632093eb6e0be9c085630d0b5af7
View
@@ -0,0 +1 @@
+onelog
View
@@ -0,0 +1,23 @@
+<?xml version="1.0" encoding="UTF-8"?>
+<project version="4">
+ <component name="CompilerConfiguration">
+ <option name="DEFAULT_COMPILER" value="Javac" />
+ <resourceExtensions />
+ <wildcardResourcePatterns>
+ <entry name="!?*.java" />
+ <entry name="!?*.form" />
+ <entry name="!?*.class" />
+ <entry name="!?*.groovy" />
+ <entry name="!?*.scala" />
+ <entry name="!?*.flex" />
+ <entry name="!?*.kt" />
+ <entry name="!?*.clj" />
+ </wildcardResourcePatterns>
+ <annotationProcessing>
+ <profile default="true" name="Default" enabled="false">
+ <processorPath useClasspath="true" />
+ </profile>
+ </annotationProcessing>
+ </component>
+</project>
+
@@ -0,0 +1,5 @@
+<component name="CopyrightManager">
+ <settings default="">
+ <module2copyright />
+ </settings>
+</component>
View
@@ -0,0 +1,5 @@
+<?xml version="1.0" encoding="UTF-8"?>
+<project version="4">
+ <component name="Encoding" useUTFGuessing="true" native2AsciiForPropertiesFiles="false" />
+</project>
+
View
@@ -0,0 +1,24 @@
+<?xml version="1.0" encoding="UTF-8"?>
+<project version="4">
+ <component name="IdProvider" IDEtalkID="5CCF2BE28D3CCD2FD737292020C4CDC5" />
+ <component name="ProjectRootManager" version="2" languageLevel="JDK_1_6" assert-keyword="true" jdk-15="true">
+ <output url="file://$PROJECT_DIR$/out" />
+ </component>
+ <component name="masterDetails">
+ <states>
+ <state key="ProjectJDKs.UI">
+ <settings>
+ <last-edited>1.6</last-edited>
+ <splitter-proportions>
+ <option name="proportions">
+ <list>
+ <option value="0.2" />
+ </list>
+ </option>
+ </splitter-proportions>
+ </settings>
+ </state>
+ </states>
+ </component>
+</project>
+
View
@@ -0,0 +1,9 @@
+<?xml version="1.0" encoding="UTF-8"?>
+<project version="4">
+ <component name="ProjectModuleManager">
+ <modules>
+ <module fileurl="file://$PROJECT_DIR$/onelog.iml" filepath="$PROJECT_DIR$/onelog.iml" />
+ </modules>
+ </component>
+</project>
+
@@ -0,0 +1,5 @@
+<component name="DependencyValidationManager">
+ <state>
+ <option name="SKIP_IMPORT_STATEMENTS" value="false" />
+ </state>
+</component>
View
@@ -0,0 +1,7 @@
+<?xml version="1.0" encoding="UTF-8"?>
+<project version="4">
+ <component name="VcsDirectoryMappings">
+ <mapping directory="" vcs="Git" />
+ </component>
+</project>
+
View

Large diffs are not rendered by default.

Oops, something went wrong.
View
@@ -0,0 +1,9 @@
+<?xml version="1.0" encoding="UTF-8"?>
+<module type="WEB_MODULE" version="4">
+ <component name="NewModuleRootManager" inherit-compiler-output="true">
+ <exclude-output />
+ <content url="file://$MODULE_DIR$" />
+ <orderEntry type="sourceFolder" forTests="false" />
+ </component>
+</module>
+
View
@@ -10,14 +10,16 @@
"homepage": "https://github.com/vjpr/onelog",
"main": "index",
"keywords": [
- "logging", "logger"
+ "logging",
+ "logger"
],
"repository": {
"type": "git",
"url": "git://github.com/vjpr/onelog.git"
},
"dependencies": {
- "underscore": "*"
+ "underscore": "*",
+ "stack-trace": "0.0.6"
},
"devDependencies": {
"log4js": "*",
@@ -26,7 +28,9 @@
"caterpillar": "*",
"tracer": "*"
},
- "licenses": [{
- "type": "MIT"
- }]
+ "licenses": [
+ {
+ "type": "MIT"
+ }
+ ]
}
View
@@ -8,7 +8,8 @@ config =
# TODO: Add a more thorough list of common methods
methods: ['debug', 'info', 'notice', 'warning', 'error', 'crit', 'alert',
'emerg', 'trace', 'log', 'warn', 'line', 'time', 'timeEnd',
- 'profile', 'assert', 'log', 'fatal', 'dir', 'start', 'stop']
+ 'profile', 'assert', 'log', 'fatal', 'dir', 'start', 'stop',
+ 'isLevelEnabled']
# Timers
# ------
@@ -38,7 +39,7 @@ class Logger
else if @logger[method]?
@logger[method] a...
else
- defaultMethod = _library.defaultLevel()
+ defaultMethod = GLOBAL.onelog._library.defaultLevel()
@logger[defaultMethod] a...
# Disable logger for a single level or all levels if no argument
# TODO: Level functionality
@@ -84,27 +85,45 @@ _library = undefined
# The logger used when no namespace is defined
_defaultLogger = undefined
+stackTrace = require 'stack-trace'
+path = require 'path'
+getCallerFile = ->
+ frame = stackTrace.get()[2]
+ #file = path.basename frame.getFileName()
+ file = frame.getFileName()
+ line = frame.getLineNumber()
+ method = frame.getFunctionName()
+ "#{frame.getTypeName()} #{file}: #{line} in #{method}()"
+
# Public API
# ------------------------------------------------------------------------------
# opts
# - methods: Custom level. If you change to a logging library that does not
# support these levels, a default level will be used.
-exports.use = (clazz, opts) ->
+exports.use = (clazz, opts = {}) ->
# Check interface of clazz
for k of Library::
if not clazz?.prototype[k]
throw new Error "
Invalid logging library prototype.
You must pass in a class with a prototype that adheres to Library.
"
- _library = new clazz
- _defaultLogger = _library.get()
+
+ # Do not allow overriding if already initialized.
+ if not GLOBAL.onelog?
+ GLOBAL.onelog or= {}
+ GLOBAL.onelog._library = _library = new clazz opts.lib
+ GLOBAL.onelog._defaultLogger = _defaultLogger = _library.get()
+ console.log "OneLog is using logging library #{clazz.name} - Initialized from #{getCallerFile arguments}"
+ else
+ _library = GLOBAL.onelog._library
+ _defaultLogger = GLOBAL.onelog._defaultLogger
# Allow custom methods for logger specified by library.
# E.g. log4js uses`logger.setLevel`
- if _library.getOpts()?
- config.methods = _.union config.methods, _library.getOpts().methods
+ if GLOBAL.onelog._library.getOpts()?
+ config.methods = _.union config.methods, GLOBAL.onelog._library.getOpts().methods
# Allow custom methods for logger passed in by user.
if opts?.methods?
@@ -115,25 +134,29 @@ exports.use = (clazz, opts) ->
# Create or get a logger instance
exports.get = (category) ->
- _library.get category
+ # Initiate default logger if none has been setup.
+ unless GLOBAL.onelog? then exports.use Console
+ GLOBAL.onelog._library.get category
# Support for logule namespaces
exports.sub = (namespaces...) ->
- _library.sub
+ GLOBAL.onelog._library.sub
exports.middleware = (opts) ->
- _library.middleware opts
+ GLOBAL.onelog._library.middleware opts
exports.getLibrary = ->
- _library?.getLibrary()
+ GLOBAL.onelog._library?.getLibrary()
# Provided library adapters
# ------------------------------------------------------------------------------
class Log4js extends Library
- constructor: ->
- @log4js = require 'log4js'
+ name: 'Log4js'
+
+ constructor: (@log4js) ->
+ unless @log4js? then @log4js = require 'log4js'
get: (category) ->
if category
@@ -157,6 +180,8 @@ class Log4js extends Library
class Logule extends Library
+ name: 'Logule'
+
constructor: ->
@logule = require 'logule'
@@ -175,6 +200,8 @@ class Logule extends Library
class Winston extends Library
+ name: 'Winston'
+
constructor: ->
@winston = require 'winston'
@@ -208,6 +235,8 @@ class Winston extends Library
class Caterpillar extends Library
+ name: 'Caterpillar'
+
constructor: ->
@caterpillar = require 'caterpillar'
@@ -227,6 +256,3 @@ exports.Log4js = Log4js
exports.Winston = Winston
exports.Caterpillar = Caterpillar
exports.Tracer = Tracer
-
-# Initiate default logger
-exports.use Console

0 comments on commit bcb2c37

Please sign in to comment.