📅 You're Invited: Meet the Socket team at RSAC (April 28 – May 1).RSVP →

serverless-logging-config

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

serverless-logging-config - npm Package Compare versions

Comparing version

to
1.0.0

class ServerlessLoggingConfig {
constructor(serverless) {
constructor (serverless) {
this.serverless = serverless

@@ -7,3 +7,3 @@ this.log = (msgs) => console.log('serverless-logging-config:', msgs)

this.hooks = {
'initialize': () => this.init(this),
initialize: () => this.init(this),
'before:package:initialize': () => this.disableFunctionLogs(this),

@@ -15,6 +15,6 @@ 'after:package:compileEvents': () => this.setLoggingConfig(this),

init() {
const settings = this.serverless.service.custom['serverless-logging-config']
init () {
const settings = this.serverless.service.custom?.['serverless-logging-config']
if (!settings) {
throw new Error(`serverless-logging-config: No custom settings found.
throw new Error(`serverless-logging-config: No custom settings found.
You need to configure this plugin by add a "serverless-logging-config" section under "custom".

@@ -37,25 +37,29 @@ For example, like this

}
}
disableFunctionLogs () {
const settings = this.serverless.service.custom['serverless-logging-config']
if (!settings.logGroupName) {
throw new Error(`serverless-logging-config: You need to set the "logGroupName".
For example:
custom:
serverless-logging-config:
enableJson: true # [Optional] set the LogFormat to JSON
logGroupName: "my-logs" # [Required] all functions to send logs to the "my-logs" log group
applicationLogLevel: DEBUG | ERROR | FATAL | INFO | TRACE | WARN
systemLogLevel: DEBUG | INFO | WARN
`)
return
}
}
disableFunctionLogs() {
const exclude = settings.useDefaultLogGroup || []
const functions = this.serverless.service.functions
Object.values(functions).forEach(x => x.disableLogs = true)
this.log('Disabled auto-generated Lambda log groups')
const functionNames = Object.keys(functions)
functionNames
.filter(x => !exclude.includes(x))
.forEach(x => {
functions[x].disableLogs = true
})
let logMsg = 'Disabled auto-generated Lambda log groups'
if (exclude.length > 0) {
logMsg += ` (excluding ${exclude.join(', ')})`
}
this.log(logMsg)
}
setLoggingConfig() {
setLoggingConfig () {
const settings = this.serverless.service.custom['serverless-logging-config']
const exclude = settings.useDefaultLogGroup || []

@@ -67,8 +71,8 @@ const template = this.serverless.service.provider.compiledCloudFormationTemplate

const updatedRoles = []
for (const func of functions) {
const isDisabled = this.isDefaultLogGroup(func, template)
functions.forEach(x => {
x.Properties.LoggingConfig = {
func.Properties.LoggingConfig = {
ApplicationLogLevel: settings.applicationLogLevel,
LogGroup: settings.logGroupName,
LogGroup: !isDisabled ? settings.logGroupName : undefined,
LogFormat: settings.enableJson === true ? 'JSON' : 'Text',

@@ -81,12 +85,19 @@ SystemLogLevel: settings.systemLogLevel

// DependsOn to be an array, so we'll set it to an empty array if it's null
if (!x.DependsOn) {
x.DependsOn = []
}
})
if (!func.DependsOn) {
func.DependsOn = []
}
}
this.log('Added LoggingConfig to all the functions.')
let logMsg = 'Added LoggingConfig to all the functions'
if (exclude.length > 0) {
logMsg += ` (excluding ${exclude.join(', ')})`
}
this.log(logMsg)
}
addIamPermissions() {
addIamPermissions () {
const settings = this.serverless.service.custom['serverless-logging-config']
if (!settings.logGroupName) {
return
}

@@ -99,3 +110,3 @@ const template = this.serverless.service.provider.compiledCloudFormationTemplate

const updatedRoles = []
const updateRole = roleLogicalId => {
const updateRole = roleLogicalId => {
if (!updatedRoles.includes(roleLogicalId)) {

@@ -107,14 +118,16 @@ const role = template.Resources[roleLogicalId]

}
role.Properties.Policies.forEach(x => {
x.PolicyDocument.Statement.forEach(stm => {
stm.Action = this.arrayify(stm.Action)
stm.Resource = this.arrayify(stm.Resource)
if (stm.Action.filter(act => act.startsWith('logs:')).length > 0) {
stm.Resource.push({
'Fn::Sub': `arn:\${AWS::Partition}:logs:\${AWS::Region}:\${AWS::AccountId}:log-group:${settings.logGroupName}:*`
})
}
})
x.PolicyDocument.Statement
.filter(stm => stm.Effect === 'Allow')
.forEach(stm => {
stm.Action = this.arrayify(stm.Action)
stm.Resource = this.arrayify(stm.Resource)
if (stm.Action.filter(act => act.startsWith('logs:')).length > 0) {
stm.Resource.push({
'Fn::Sub': `arn:\${AWS::Partition}:logs:\${AWS::Region}:\${AWS::AccountId}:log-group:${settings.logGroupName}:*`
})
}
})
})

@@ -138,3 +151,3 @@ }

arrayify(obj) {
arrayify (obj) {
if (Array.isArray(obj)) {

@@ -148,4 +161,21 @@ return obj

}
isDefaultLogGroup (x, template) {
if (!x.DependsOn) {
return false
}
for (const dep of x.DependsOn) {
if (dep.endsWith('LogGroup')) {
const logGroup = template.Resources[dep]
if (logGroup.Type === 'AWS::Logs::LogGroup') {
return true
}
}
}
return false
}
}
module.exports = ServerlessLoggingConfig
module.exports = ServerlessLoggingConfig

@@ -7,10 +7,45 @@ {

},
"version": "0.1.2",
"version": "1.0.0",
"description": "Lets you configure custom log group, JSON logging, and other recent logging changes announce in Nov 2023.",
"main": "index.js",
"scripts": {
"lint": "eslint .",
"test": "jest",
"semantic-release": "semantic-release"
},
"keywords": [],
"author": "Yan Cui",
"license": "MIT"
"license": "MIT",
"devDependencies": {
"@commitlint/cli": "^18.4.3",
"@commitlint/config-conventional": "^18.4.3",
"@types/jest": "^29.5.10",
"coveralls": "^3.1.1",
"eslint": "^8.54.0",
"eslint-config-standard": "^17.1.0",
"eslint-plugin-import": "^2.29.0",
"eslint-plugin-node": "^11.1.0",
"eslint-plugin-promise": "^6.1.1",
"eslint-plugin-standard": "^5.0.0",
"husky": "^8.0.3",
"jest": "^29.7.0",
"lint-staged": "^15.1.0",
"semantic-release": "^22.0.8"
},
"husky": {
"hooks": {
"pre-commit": "lint-staged",
"commit-msg": "commitlint -E HUSKY_GIT_PARAMS"
}
},
"lint-staged": {
"*.js": [
"eslint"
]
},
"release": {
"branches": [
"main"
]
}
}

@@ -30,5 +30,8 @@ # serverless-logging-config

enableJson: true # [Optional] if enabled, set the LogFormat to JSON
logGroupName: my-logs # [Required] all functions will send logs this log group
logGroupName: my-logs # [Optional] if set, all functions will send logs this log group
applicationLogLevel: INFO # [Optional] valid values are DEBUG, ERROR, FATAL, INFO, TRACE and WARN
systemLogLevel: INFO # [Optional] valid values are DEBUG, INFO and WARN
useDefaultLogGroup: # [Optional] these functions would keep logging to their default log group
- function1
- function2
```

@@ -35,0 +38,0 @@