Skip to content

Commit

Permalink
feat: first commit
Browse files Browse the repository at this point in the history
  • Loading branch information
simenandre committed Apr 12, 2022
1 parent 3f6fe06 commit 86dceda
Show file tree
Hide file tree
Showing 14 changed files with 4,749 additions and 1 deletion.
12 changes: 12 additions & 0 deletions .editorconfig
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
# EditorConfig is awesome: https://EditorConfig.org

# top-most EditorConfig file
root = true

[*]
indent_style = space
indent_size = 2
end_of_line = lf
charset = utf-8
trim_trailing_whitespace = false
insert_final_newline = false
33 changes: 33 additions & 0 deletions .github/workflows/workflow.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
name: Release
on:
push:
branches:
- main

jobs:
build:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v3
- name: Use Node.js 16.x
uses: actions/setup-node@v3
with:
node-version: 16.x
registry-url: 'https://registry.npmjs.org'
cache: yarn
- run: yarn install
- run: yarn run build
- run: yarn test --passWithNoTests
env:
CI: true
- uses: codecov/codecov-action@v2
- uses: GoogleCloudPlatform/release-please-action@v3.1.3
id: release
with:
token: ${{ secrets.GITHUB_TOKEN }}
release-type: node
package-name: pino-gcl-slack-transport
- if: ${{ steps.release.outputs.release_created }}
run: yarn publish
env:
NODE_AUTH_TOKEN: ${{ secrets.NPM_TOKEN }}
104 changes: 104 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,104 @@
# Logs
logs
*.log
npm-debug.log*
yarn-debug.log*
yarn-error.log*
lerna-debug.log*

# Diagnostic reports (https://nodejs.org/api/report.html)
report.[0-9]*.[0-9]*.[0-9]*.[0-9]*.json

# Runtime data
pids
*.pid
*.seed
*.pid.lock

# Directory for instrumented libs generated by jscoverage/JSCover
lib-cov

# Coverage directory used by tools like istanbul
coverage
*.lcov

# nyc test coverage
.nyc_output

# Grunt intermediate storage (https://gruntjs.com/creating-plugins#storing-task-files)
.grunt

# Bower dependency directory (https://bower.io/)
bower_components

# node-waf configuration
.lock-wscript

# Compiled binary addons (https://nodejs.org/api/addons.html)
build/Release

# Dependency directories
node_modules/
jspm_packages/

# TypeScript v1 declaration files
typings/

# TypeScript cache
*.tsbuildinfo

# Optional npm cache directory
.npm

# Optional eslint cache
.eslintcache

# Microbundle cache
.rpt2_cache/
.rts2_cache_cjs/
.rts2_cache_es/
.rts2_cache_umd/

# Optional REPL history
.node_repl_history

# Output of 'npm pack'
*.tgz

# Yarn Integrity file
.yarn-integrity

# dotenv environment variables file
.env
.env.test

# parcel-bundler cache (https://parceljs.org/)
.cache

# Next.js build output
.next

# Nuxt.js build / generate output
.nuxt
dist

# Gatsby files
.cache/
# Comment in the public line in if your project uses Gatsby and *not* Next.js
# https://nextjs.org/blog/next-9-1#public-directory-support
# public

# vuepress build output
.vuepress/dist

# Serverless directories
.serverless/

# FuseBox cache
.fusebox/

# DynamoDB Local files
.dynamodb/

# TernJS port file
.tern-port
7 changes: 6 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
@@ -1 +1,6 @@
# slack-logger
# pino-gcl-slack-transport

This is a Pino transport for [google-cloud-logger-slack]

[google-cloud-logger-slack]:
https://github.com/bjerkio/google-cloud-logger-slack
15 changes: 15 additions & 0 deletions jest.config.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
module.exports = {
preset: 'ts-jest',
testPathIgnorePatterns: [
'.*.utils.ts',
'.*/fixtures.ts',
'.*/*.fixtures.ts',
'.*/dist/.*',
'.*dist.*',
],
globals: {
'ts-jest': {
tsconfig: 'tsconfig.jest.json',
},
},
};
50 changes: 50 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
{
"name": "pino-gcl-slack-transport",
"main": "./dist/cjs/transport.js",
"module": "./dist/transport.js",
"source": "src/transport.ts",
"types": "./dist/index.d.ts",
"prettier": "@cobraz/prettier",
"eslintConfig": {
"extends": "@bjerk/eslint-config",
"rules": {
"import/no-default-export": "off"
}
},
"keywords": [
"pino",
"transport",
"google",
"app",
"cloud",
"logger",
"logging",
"stackdriver",
"cloud-functions"
],
"scripts": {
"dev": "tsnd --respawn src/main.ts",
"lint": "eslint . --ext .ts,.tsx",
"test": "jest --coverage src/**/*",
"build": "tsc && tsc -p tsconfig.cjs.json"
},
"dependencies": {
"@google-cloud/logging": "^9.8.2",
"pino": "^7.10.0",
"pino-abstract-transport": "^0.5.0",
"sonic-boom": "^2.7.0"
},
"devDependencies": {
"@bjerk/eslint-config": "^2.0.1",
"@cobraz/prettier": "^2.0.0",
"@tsconfig/node16": "^1.0.2",
"@types/jest": "^27.4.1",
"@types/node": "16",
"jest": "^27.5.1",
"prettier": "^2.5.1",
"ts-jest": "^27.1.3",
"ts-node": "^10.4.0",
"typescript": "^4.5.4"
},
"license": "Apache-2.0"
}
29 changes: 29 additions & 0 deletions src/slack-logger.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
import { Logging } from '@google-cloud/logging';
import { Destination, Options } from './types';

const logging = new Logging();
const logSync = logging.logSync('slack');

export const sendLogMessages = async (
destination: Destination,
log: Record<string, any>,
options: Options,
): Promise<void> => {
const metadata = {
severity: 'NOTICE',
'logging.googleapis.com/labels': { type: 'pino-gcl-slack-transport' },
'logging.googleapis.com/operation': {
producer: 'github.com/bjerkio/google-cloud-logger-slack@v1',
},
};

const parsedJson = logSync
.entry(metadata, {
slack: {
channel: options.defaultChannel,
...log,
},
})
.toJSON();
destination.write(JSON.stringify(parsedJson));
};
27 changes: 27 additions & 0 deletions src/transport.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
import { Writable } from 'stream';
import build from 'pino-abstract-transport';
import { sendLogMessages } from './slack-logger';
import { Destination, Options } from './types';
import { buildSafeSonicBoom } from './utils';

export const createTransport = (opts: Options): Writable => {
let destination: Destination;

if (
typeof opts.destination === 'object' &&
typeof opts.destination.write === 'function'
) {
destination = opts.destination as Destination;
} else {
destination = buildSafeSonicBoom({
dest: (opts.destination as string | number) || 1,
append: opts.append,
mkdir: opts.mkdir,
sync: opts.sync, // by default sonic will be async
});
}

return build(source => sendLogMessages(destination, source, opts));
};

export default createTransport;
36 changes: 36 additions & 0 deletions src/types.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
import { DestinationStream } from 'pino';

export interface Options {
/**
* This option is used when channel isn't defined
* in the log entry. Read more about the Slack object.
*/
defaultChannel?: string;

/**
* The file, file descriptor, or stream to write to. Defaults to 1 (stdout).
* @default 1
*/
destination?: string | number | DestinationStream | NodeJS.WritableStream;

/**
* Opens the file with the 'a' flag.
* @default true
*/
append?: boolean;
/**
* Ensure directory for destination file exists.
* @default false
*/
mkdir?: boolean;

/**
* Makes messaging synchronous.
* @default false
*/
sync?: boolean;
}

export type Destination = {
write(string: string): boolean;
}
20 changes: 20 additions & 0 deletions src/utils.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
import SonicBoom, { SonicBoomOpts } from 'sonic-boom';

// eslint-disable-next-line @typescript-eslint/no-empty-function
const noop: any = () => {};

export const buildSafeSonicBoom = (opts: SonicBoomOpts): SonicBoom => {
const stream = new SonicBoom(opts);
const filterBrokenPipe = (err: any) => {
if (err.code === 'EPIPE') {
stream.write = noop;
stream.end = noop;
stream.flushSync = noop;
stream.destroy = noop;
return;
}
stream.removeListener('error', filterBrokenPipe);
};
stream.on('error', filterBrokenPipe);
return stream;
};
8 changes: 8 additions & 0 deletions tsconfig.cjs.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
{
"extends": "./tsconfig.json",
"compilerOptions": {
"declaration": false,
"module": "CommonJS",
"outDir": "./dist/cjs"
}
}
6 changes: 6 additions & 0 deletions tsconfig.jest.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
{
"extends": "./tsconfig.json",
"compilerOptions": {
"sourceMap": true
}
}
8 changes: 8 additions & 0 deletions tsconfig.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
{
"extends": "@tsconfig/node16/tsconfig.json",
"compilerOptions": {
"outDir": "dist",
"declaration": true
},
"include": ["src/**/*.ts"]
}
Loading

0 comments on commit 86dceda

Please sign in to comment.