Security News
Research
Data Theft Repackaged: A Case Study in Malicious Wrapper Packages on npm
The Socket Research Team breaks down a malicious wrapper package that uses obfuscation to harvest credentials and exfiltrate sensitive data.
@optimizely/js-sdk-logging
Advanced tools
@optimizely/js-sdk-logging is a logging utility for the Optimizely JavaScript SDK. It provides a way to log messages with different levels of severity and can be integrated with various logging frameworks.
Basic Logging
This feature allows you to create a logger and log messages with different severity levels such as info and error.
const logging = require('@optimizely/js-sdk-logging');
const logger = logging.getLogger('my-logger');
logger.info('This is an info message');
logger.error('This is an error message');
Custom Log Levels
This feature allows you to set custom log levels for your logger, enabling more granular control over what gets logged.
const logging = require('@optimizely/js-sdk-logging');
const logger = logging.getLogger('my-logger');
logger.setLogLevel(logging.LogLevel.DEBUG);
logger.debug('This is a debug message');
Integrating with External Logging Frameworks
This feature allows you to integrate @optimizely/js-sdk-logging with external logging frameworks like Winston, enabling you to leverage their advanced logging capabilities.
const logging = require('@optimizely/js-sdk-logging');
const winston = require('winston');
const logger = logging.getLogger('my-logger');
const winstonLogger = winston.createLogger({
level: 'info',
transports: [
new winston.transports.Console()
]
});
logger.addLogHandler((logEvent) => {
winstonLogger.log({
level: logEvent.level,
message: logEvent.message
});
});
logger.info('This message will be logged by Winston');
Winston is a versatile logging library for Node.js that supports multiple transports, log levels, and formats. It is more feature-rich compared to @optimizely/js-sdk-logging and is widely used in the Node.js community.
Bunyan is a simple and fast JSON logging library for Node.js services. It provides a structured logging approach and is known for its performance. Compared to @optimizely/js-sdk-logging, Bunyan offers more advanced features for JSON logging.
Log4js is a logging library inspired by Apache Log4j. It provides a variety of appenders for different logging outputs and supports hierarchical loggers. Log4js offers more flexibility and configuration options compared to @optimizely/js-sdk-logging.
Provides a centralized LogManager and errorHandler for Javascript SDK packages.
npm install @optimizely/js-sdk-logging
ConsoleLogHandler
or NoopLogHandler
LogManager.getLogger(name)
import { getLogger } from '@optimizely/js-sdk-logging'
const logger = getLogger('myModule')
logger.log('warn', 'this is a warning')
logger.debug('string interpolation is easy and %s', 'lazily evaluated')
logger.info('info logging')
logger.warn('this is a warning')
logger.error('this is an error')
// `info` `warn` `debug` and `error` all support passing an Error as the last argument
// this will call the registered errorHandler
logger.error('an error occurred: %s', ex.message)
// also Error passes to errorHandler.handleError(ex)
logger.error('an error occurred: %s', ex.message, ex)
// if no message is passed will log `ex.message`
logger.error(ex)
import { LogLevel, setLogLevel } from '@optimizely/js-sdk-logging'
// can use enum
setLogLevel(LogLevel.ERROR)
// can also use a string (lowercase or uppercase)
setLogLevel('debug')
setLogLevel('info')
setLogLevel('warn')
setLogLevel('error')
import { setLogHandler, ConsoleLogHandler } from '@optimizely/js-sdk-logging'
const handler = new ConsoleLogHandler({
logLevel: 'error',
prefix: '[My custom prefix]', // defaults to "[OPTIMIZELY]"
})
setLogHandler(handler)
Perhaps you want to integrate Optimizely with your own logging system or use an existing library.
A valid LogHandler
is anything that implements this interface
interface LogHandler {
log(level: LogLevel, message: string): void
}
Example: integrating with Winston
import winston from 'winston'
import { setLogHandler, LogLevel } from '@optimizely/js-sdk-logging'
const winstonLogger = winston.createLogger({
level: 'info',
format: winston.format.json(),
defaultMeta: { service: 'optimizely' },
transports: [
new winston.transports.File({ filename: 'combined.log' }),
],
})
/**
* Convert from optimizely log levels to winston
*/
function convertLogLevels(level) {
switch(level) {
case LogLevel.DEBUG:
return 'debug'
case LogLevel.INFO:
return 'info'
case LogLevel.WARNING:
return 'warning'
case LogLevel.ERROR:
return 'error'
default:
return 'silly'
}
}
setLogHandler({
log(level, message) {
winstoLogger.log({
level: convertLogLevels(level),
message,
})
}
})
interface LoggerFacade {
log(level: LogLevel | string, message: string): void
info(message: string | Error, ...splat: any[]): void
debug(message: string | Error, ...splat: any[]): void
warn(message: string | Error, ...splat: any[]): void
error(message: string | Error, ...splat: any[]): void
}
interface LogManager {
getLogger(name?: string): LoggerFacade
}
interface LogHandler {
log(level: LogLevel, message: string): void
}
FAQs
Optimizely Full Stack Core Logging
The npm package @optimizely/js-sdk-logging receives a total of 96,962 weekly downloads. As such, @optimizely/js-sdk-logging popularity was classified as popular.
We found that @optimizely/js-sdk-logging demonstrated a not healthy version release cadence and project activity because the last version was released a year ago. It has 1 open source maintainer collaborating on the project.
Did you know?
Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.
Security News
Research
The Socket Research Team breaks down a malicious wrapper package that uses obfuscation to harvest credentials and exfiltrate sensitive data.
Research
Security News
Attackers used a malicious npm package typosquatting a popular ESLint plugin to steal sensitive data, execute commands, and exploit developer systems.
Security News
The Ultralytics' PyPI Package was compromised four times in one weekend through GitHub Actions cache poisoning and failure to rotate previously compromised API tokens.