@@ -2042,7 +2042,40 @@ var type = function (val) {
20422042 return typeof val === 'undefined' ? 'undefined' : _typeof ( val ) ;
20432043} ;
20442044
2045- var log = function log ( s ) {
2045+ var logLevel = 'INFO' ;
2046+
2047+ var logLevels = {
2048+ DISABLE : 0 ,
2049+ ERROR : 1 ,
2050+ WARN : 2 ,
2051+ INFO : 3
2052+ } ;
2053+
2054+ var setLogLevel = function setLogLevel ( logLevelName ) {
2055+ logLevel = logLevels [ logLevelName ] ;
2056+ } ;
2057+
2058+ var log = {
2059+ error : function error ( s ) {
2060+ if ( logLevel >= logLevels . ERROR ) {
2061+ _log ( s ) ;
2062+ }
2063+ } ,
2064+
2065+ warn : function warn ( s ) {
2066+ if ( logLevel >= logLevels . WARN ) {
2067+ _log ( s ) ;
2068+ }
2069+ } ,
2070+
2071+ info : function info ( s ) {
2072+ if ( logLevel >= logLevels . INFO ) {
2073+ _log ( s ) ;
2074+ }
2075+ }
2076+ } ;
2077+
2078+ var _log = function _log ( s ) {
20462079 try {
20472080 console . log ( '[Amplitude] ' + s ) ;
20482081 } catch ( e ) {
@@ -2091,7 +2124,7 @@ var _truncateValue = function _truncateValue(value) {
20912124
20922125var validateInput = function validateInput ( input , name , expectedType ) {
20932126 if ( type ( input ) !== expectedType ) {
2094- log ( 'Invalid ' + name + ' input type. Expected ' + expectedType + ' but received ' + type ( input ) ) ;
2127+ log . error ( 'Invalid ' + name + ' input type. Expected ' + expectedType + ' but received ' + type ( input ) ) ;
20952128 return false ;
20962129 }
20972130 return true ;
@@ -2101,12 +2134,12 @@ var validateInput = function validateInput(input, name, expectedType) {
21012134var validateProperties = function validateProperties ( properties ) {
21022135 var propsType = type ( properties ) ;
21032136 if ( propsType !== 'object' ) {
2104- log ( 'Error: invalid properties format. Expecting Javascript object, received ' + propsType + ', ignoring' ) ;
2137+ log . error ( 'Error: invalid properties format. Expecting Javascript object, received ' + propsType + ', ignoring' ) ;
21052138 return { } ;
21062139 }
21072140
21082141 if ( Object . keys ( properties ) . length > constants . MAX_PROPERTY_KEYS ) {
2109- log ( 'Error: too many properties (more than 1000), ignoring' ) ;
2142+ log . error ( 'Error: too many properties (more than 1000), ignoring' ) ;
21102143 return { } ;
21112144 }
21122145
@@ -2121,7 +2154,7 @@ var validateProperties = function validateProperties(properties) {
21212154 var keyType = type ( key ) ;
21222155 if ( keyType !== 'string' ) {
21232156 key = String ( key ) ;
2124- log ( 'WARNING: Non-string property key, received type ' + keyType + ', coercing to string "' + key + '"' ) ;
2157+ log . warn ( 'WARNING: Non-string property key, received type ' + keyType + ', coercing to string "' + key + '"' ) ;
21252158 }
21262159
21272160 // validate value
@@ -2139,19 +2172,19 @@ var invalidValueTypes = ['null', 'nan', 'undefined', 'function', 'arguments', 'r
21392172var validatePropertyValue = function validatePropertyValue ( key , value ) {
21402173 var valueType = type ( value ) ;
21412174 if ( invalidValueTypes . indexOf ( valueType ) !== - 1 ) {
2142- log ( 'WARNING: Property key "' + key + '" with invalid value type ' + valueType + ', ignoring' ) ;
2175+ log . warn ( 'WARNING: Property key "' + key + '" with invalid value type ' + valueType + ', ignoring' ) ;
21432176 value = null ;
21442177 } else if ( valueType === 'error' ) {
21452178 value = String ( value ) ;
2146- log ( 'WARNING: Property key "' + key + '" with value type error, coercing to ' + value ) ;
2179+ log . warn ( 'WARNING: Property key "' + key + '" with value type error, coercing to ' + value ) ;
21472180 } else if ( valueType === 'array' ) {
21482181 // check for nested arrays or objects
21492182 var arrayCopy = [ ] ;
21502183 for ( var i = 0 ; i < value . length ; i ++ ) {
21512184 var element = value [ i ] ;
21522185 var elemType = type ( element ) ;
21532186 if ( elemType === 'array' || elemType === 'object' ) {
2154- log ( 'WARNING: Cannot have ' + elemType + ' nested in an array property value, skipping' ) ;
2187+ log . warn ( 'WARNING: Cannot have ' + elemType + ' nested in an array property value, skipping' ) ;
21552188 continue ;
21562189 }
21572190 arrayCopy . push ( validatePropertyValue ( key , element ) ) ;
@@ -2166,7 +2199,7 @@ var validatePropertyValue = function validatePropertyValue(key, value) {
21662199var validateGroups = function validateGroups ( groups ) {
21672200 var groupsType = type ( groups ) ;
21682201 if ( groupsType !== 'object' ) {
2169- log ( 'Error: invalid groups format. Expecting Javascript object, received ' + groupsType + ', ignoring' ) ;
2202+ log . error ( 'Error: invalid groups format. Expecting Javascript object, received ' + groupsType + ', ignoring' ) ;
21702203 return { } ;
21712204 }
21722205
@@ -2181,7 +2214,7 @@ var validateGroups = function validateGroups(groups) {
21812214 var keyType = type ( key ) ;
21822215 if ( keyType !== 'string' ) {
21832216 key = String ( key ) ;
2184- log ( 'WARNING: Non-string groupType, received type ' + keyType + ', coercing to string "' + key + '"' ) ;
2217+ log . warn ( 'WARNING: Non-string groupType, received type ' + keyType + ', coercing to string "' + key + '"' ) ;
21852218 }
21862219
21872220 // validate value
@@ -2201,7 +2234,7 @@ var validateGroupName = function validateGroupName(key, groupName) {
22012234 }
22022235 if ( groupNameType === 'date' || groupNameType === 'number' || groupNameType === 'boolean' ) {
22032236 groupName = String ( groupName ) ;
2204- log ( 'WARNING: Non-string groupName, received type ' + groupNameType + ', coercing to string "' + groupName + '"' ) ;
2237+ log . warn ( 'WARNING: Non-string groupName, received type ' + groupNameType + ', coercing to string "' + groupName + '"' ) ;
22052238 return groupName ;
22062239 }
22072240 if ( groupNameType === 'array' ) {
@@ -2211,19 +2244,19 @@ var validateGroupName = function validateGroupName(key, groupName) {
22112244 var element = groupName [ i ] ;
22122245 var elemType = type ( element ) ;
22132246 if ( elemType === 'array' || elemType === 'object' ) {
2214- log ( 'WARNING: Skipping nested ' + elemType + ' in array groupName' ) ;
2247+ log . warn ( 'WARNING: Skipping nested ' + elemType + ' in array groupName' ) ;
22152248 continue ;
22162249 } else if ( elemType === 'string' ) {
22172250 arrayCopy . push ( element ) ;
22182251 } else if ( elemType === 'date' || elemType === 'number' || elemType === 'boolean' ) {
22192252 element = String ( element ) ;
2220- log ( 'WARNING: Non-string groupName, received type ' + elemType + ', coercing to string "' + element + '"' ) ;
2253+ log . warn ( 'WARNING: Non-string groupName, received type ' + elemType + ', coercing to string "' + element + '"' ) ;
22212254 arrayCopy . push ( element ) ;
22222255 }
22232256 }
22242257 return arrayCopy ;
22252258 }
2226- log ( 'WARNING: Non-string groupName, received type ' + groupNameType + '. Please use strings or array of strings for groupName' ) ;
2259+ log . warn ( 'WARNING: Non-string groupName, received type ' + groupNameType + '. Please use strings or array of strings for groupName' ) ;
22272260} ;
22282261
22292262// parses the value of a url param (for example ?gclid=1234&...)
@@ -2235,6 +2268,7 @@ var getQueryParam = function getQueryParam(name, query) {
22352268} ;
22362269
22372270var utils = {
2271+ setLogLevel : setLogLevel ,
22382272 log : log ,
22392273 isEmptyString : isEmptyString ,
22402274 getQueryParam : getQueryParam ,
@@ -2617,7 +2651,7 @@ Identify.prototype.add = function (property, value) {
26172651 if ( type ( value ) === 'number' || type ( value ) === 'string' ) {
26182652 this . _addOperation ( AMP_OP_ADD , property , value ) ;
26192653 } else {
2620- utils . log ( 'Unsupported type for value: ' + type ( value ) + ', expecting number or string' ) ;
2654+ utils . log . error ( 'Unsupported type for value: ' + type ( value ) + ', expecting number or string' ) ;
26212655 }
26222656 return this ;
26232657} ;
@@ -2652,7 +2686,7 @@ Identify.prototype.append = function (property, value) {
26522686Identify . prototype . clearAll = function ( ) {
26532687 if ( Object . keys ( this . userPropertiesOperations ) . length > 0 ) {
26542688 if ( ! this . userPropertiesOperations . hasOwnProperty ( AMP_OP_CLEAR_ALL ) ) {
2655- utils . log ( 'Need to send $clearAll on its own Identify object without any other operations, skipping $clearAll' ) ;
2689+ utils . log . error ( 'Need to send $clearAll on its own Identify object without any other operations, skipping $clearAll' ) ;
26562690 }
26572691 return this ;
26582692 }
@@ -2735,13 +2769,13 @@ Identify.prototype.unset = function (property) {
27352769Identify . prototype . _addOperation = function ( operation , property , value ) {
27362770 // check that the identify doesn't already contain a clearAll
27372771 if ( this . userPropertiesOperations . hasOwnProperty ( AMP_OP_CLEAR_ALL ) ) {
2738- utils . log ( 'This identify already contains a $clearAll operation, skipping operation ' + operation ) ;
2772+ utils . log . error ( 'This identify already contains a $clearAll operation, skipping operation ' + operation ) ;
27392773 return ;
27402774 }
27412775
27422776 // check that property wasn't already used in this Identify
27432777 if ( this . properties . indexOf ( property ) !== - 1 ) {
2744- utils . log ( 'User property "' + property + '" already used in this identify, skipping operation ' + operation ) ;
2778+ utils . log . error ( 'User property "' + property + '" already used in this identify, skipping operation ' + operation ) ;
27452779 return ;
27462780 }
27472781
@@ -4539,9 +4573,9 @@ var Revenue = function Revenue() {
45394573 */
45404574Revenue . prototype . setProductId = function setProductId ( productId ) {
45414575 if ( type ( productId ) !== 'string' ) {
4542- utils . log ( 'Unsupported type for productId: ' + type ( productId ) + ', expecting string' ) ;
4576+ utils . log . error ( 'Unsupported type for productId: ' + type ( productId ) + ', expecting string' ) ;
45434577 } else if ( utils . isEmptyString ( productId ) ) {
4544- utils . log ( 'Invalid empty productId' ) ;
4578+ utils . log . error ( 'Invalid empty productId' ) ;
45454579 } else {
45464580 this . _productId = productId ;
45474581 }
@@ -4558,7 +4592,7 @@ Revenue.prototype.setProductId = function setProductId(productId) {
45584592 */
45594593Revenue . prototype . setQuantity = function setQuantity ( quantity ) {
45604594 if ( type ( quantity ) !== 'number' ) {
4561- utils . log ( 'Unsupported type for quantity: ' + type ( quantity ) + ', expecting number' ) ;
4595+ utils . log . error ( 'Unsupported type for quantity: ' + type ( quantity ) + ', expecting number' ) ;
45624596 } else {
45634597 this . _quantity = parseInt ( quantity ) ;
45644598 }
@@ -4576,7 +4610,7 @@ Revenue.prototype.setQuantity = function setQuantity(quantity) {
45764610 */
45774611Revenue . prototype . setPrice = function setPrice ( price ) {
45784612 if ( type ( price ) !== 'number' ) {
4579- utils . log ( 'Unsupported type for price: ' + type ( price ) + ', expecting number' ) ;
4613+ utils . log . error ( 'Unsupported type for price: ' + type ( price ) + ', expecting number' ) ;
45804614 } else {
45814615 this . _price = price ;
45824616 }
@@ -4593,7 +4627,7 @@ Revenue.prototype.setPrice = function setPrice(price) {
45934627 */
45944628Revenue . prototype . setRevenueType = function setRevenueType ( revenueType ) {
45954629 if ( type ( revenueType ) !== 'string' ) {
4596- utils . log ( 'Unsupported type for revenueType: ' + type ( revenueType ) + ', expecting string' ) ;
4630+ utils . log . error ( 'Unsupported type for revenueType: ' + type ( revenueType ) + ', expecting string' ) ;
45974631 } else {
45984632 this . _revenueType = revenueType ;
45994633 }
@@ -4611,7 +4645,7 @@ Revenue.prototype.setRevenueType = function setRevenueType(revenueType) {
46114645*/
46124646Revenue . prototype . setEventProperties = function setEventProperties ( eventProperties ) {
46134647 if ( type ( eventProperties ) !== 'object' ) {
4614- utils . log ( 'Unsupported type for eventProperties: ' + type ( eventProperties ) + ', expecting object' ) ;
4648+ utils . log . error ( 'Unsupported type for eventProperties: ' + type ( eventProperties ) + ', expecting object' ) ;
46154649 } else {
46164650 this . _properties = utils . validateProperties ( eventProperties ) ;
46174651 }
@@ -4623,7 +4657,7 @@ Revenue.prototype.setEventProperties = function setEventProperties(eventProperti
46234657 */
46244658Revenue . prototype . _isValidRevenue = function _isValidRevenue ( ) {
46254659 if ( type ( this . _price ) !== 'number' ) {
4626- utils . log ( 'Invalid revenue, need to set price field' ) ;
4660+ utils . log . error ( 'Invalid revenue, need to set price field' ) ;
46274661 return false ;
46284662 }
46294663 return true ;
@@ -5670,7 +5704,7 @@ AmplitudeClient.prototype.Revenue = Revenue;
56705704 */
56715705AmplitudeClient . prototype . init = function init ( apiKey , opt_userId , opt_config , opt_callback ) {
56725706 if ( type ( apiKey ) !== 'string' || utils . isEmptyString ( apiKey ) ) {
5673- utils . log ( 'Invalid apiKey. Please re-initialize with a valid apiKey' ) ;
5707+ utils . log . error ( 'Invalid apiKey. Please re-initialize with a valid apiKey' ) ;
56745708 return ;
56755709 }
56765710
@@ -5714,6 +5748,10 @@ AmplitudeClient.prototype.init = function init(apiKey, opt_userId, opt_config, o
57145748 }
57155749 }
57165750
5751+ if ( type ( this . options . logLevel ) === 'string' ) {
5752+ utils . setLogLevel ( this . options . logLevel ) ;
5753+ }
5754+
57175755 var now = new Date ( ) . getTime ( ) ;
57185756 if ( ! this . _sessionId || ! this . _lastEventTime || now - this . _lastEventTime > this . options . sessionTimeout ) {
57195757 this . _newSession = true ;
@@ -5734,7 +5772,7 @@ AmplitudeClient.prototype.init = function init(apiKey, opt_userId, opt_config, o
57345772
57355773 this . _sendEventsIfReady ( ) ; // try sending unsent events
57365774 } catch ( e ) {
5737- utils . log ( e ) ;
5775+ utils . log . error ( e ) ;
57385776 } finally {
57395777 if ( type ( opt_callback ) === 'function' ) {
57405778 opt_callback ( this ) ;
@@ -5812,7 +5850,7 @@ AmplitudeClient.prototype.runQueuedFunctions = function () {
58125850 */
58135851AmplitudeClient . prototype . _apiKeySet = function _apiKeySet ( methodName ) {
58145852 if ( utils . isEmptyString ( this . options . apiKey ) ) {
5815- utils . log ( 'Invalid apiKey. Please set a valid apiKey with init() before calling ' + methodName ) ;
5853+ utils . log . error ( 'Invalid apiKey. Please set a valid apiKey with init() before calling ' + methodName ) ;
58165854 return false ;
58175855 }
58185856 return true ;
@@ -5837,7 +5875,7 @@ AmplitudeClient.prototype._loadSavedUnsentEvents = function _loadSavedUnsentEven
58375875 }
58385876 } catch ( e ) { }
58395877 }
5840- utils . log ( 'Unable to load ' + unsentKey + ' events. Restart with a new empty queue.' ) ;
5878+ utils . log . error ( 'Unable to load ' + unsentKey + ' events. Restart with a new empty queue.' ) ;
58415879 return [ ] ;
58425880} ;
58435881
@@ -6185,7 +6223,7 @@ AmplitudeClient.prototype.setDomain = function setDomain(domain) {
61856223 _loadCookieData ( this ) ;
61866224 _saveCookieData ( this ) ;
61876225 } catch ( e ) {
6188- utils . log ( e ) ;
6226+ utils . log . error ( e ) ;
61896227 }
61906228} ;
61916229
@@ -6200,7 +6238,7 @@ AmplitudeClient.prototype.setUserId = function setUserId(userId) {
62006238 this . options . userId = userId !== undefined && userId !== null && '' + userId || null ;
62016239 _saveCookieData ( this ) ;
62026240 } catch ( e ) {
6203- utils . log ( e ) ;
6241+ utils . log . error ( e ) ;
62046242 }
62056243} ;
62066244
@@ -6243,7 +6281,7 @@ AmplitudeClient.prototype.setOptOut = function setOptOut(enable) {
62436281 this . options . optOut = enable ;
62446282 _saveCookieData ( this ) ;
62456283 } catch ( e ) {
6246- utils . log ( e ) ;
6284+ utils . log . error ( e ) ;
62476285 }
62486286} ;
62496287
@@ -6256,7 +6294,7 @@ AmplitudeClient.prototype.setSessionId = function setSessionId(sessionId) {
62566294 this . _sessionId = sessionId ;
62576295 _saveCookieData ( this ) ;
62586296 } catch ( e ) {
6259- utils . log ( e ) ;
6297+ utils . log . error ( e ) ;
62606298 }
62616299} ;
62626300
@@ -6290,7 +6328,7 @@ AmplitudeClient.prototype.setDeviceId = function setDeviceId(deviceId) {
62906328 _saveCookieData ( this ) ;
62916329 }
62926330 } catch ( e ) {
6293- utils . log ( e ) ;
6331+ utils . log . error ( e ) ;
62946332 }
62956333} ;
62966334
@@ -6382,7 +6420,7 @@ AmplitudeClient.prototype.identify = function (identify_obj, opt_callback) {
63826420 return this . _logEvent ( constants . IDENTIFY_EVENT , null , null , identify_obj . userPropertiesOperations , null , null , opt_callback ) ;
63836421 }
63846422 } else {
6385- utils . log ( 'Invalid identify input type. Expected Identify object but saw ' + type ( identify_obj ) ) ;
6423+ utils . log . error ( 'Invalid identify input type. Expected Identify object but saw ' + type ( identify_obj ) ) ;
63866424 }
63876425
63886426 if ( type ( opt_callback ) === 'function' ) {
@@ -6480,7 +6518,7 @@ AmplitudeClient.prototype._logEvent = function _logEvent(eventType, eventPropert
64806518
64816519 return eventId ;
64826520 } catch ( e ) {
6483- utils . log ( e ) ;
6521+ utils . log . error ( e ) ;
64846522 }
64856523} ;
64866524
@@ -6594,7 +6632,7 @@ AmplitudeClient.prototype.logRevenueV2 = function logRevenueV2(revenue_obj) {
65946632 return this . logEvent ( constants . REVENUE_EVENT , revenue_obj . _toJSONObject ( ) ) ;
65956633 }
65966634 } else {
6597- utils . log ( 'Invalid revenue input type. Expected Revenue object but saw ' + type ( revenue_obj ) ) ;
6635+ utils . log . error ( 'Invalid revenue input type. Expected Revenue object but saw ' + type ( revenue_obj ) ) ;
65986636 }
65996637} ;
66006638
@@ -6743,7 +6781,7 @@ AmplitudeClient.prototype._mergeEventsAndIdentifys = function _mergeEventsAndIde
67436781 // case 0: no events or identifys left
67446782 // note this should not happen, this means we have less events and identifys than expected
67456783 if ( noEvents && noIdentifys ) {
6746- utils . log ( 'Merging Events and Identifys, less events and identifys than expected' ) ;
6784+ utils . log . error ( 'Merging Events and Identifys, less events and identifys than expected' ) ;
67476785 break ;
67486786 }
67496787
0 commit comments