Skip to content

Commit

Permalink
Merge pull request #83 from osstotalsoft/add-apollo-logger
Browse files Browse the repository at this point in the history
Replaced local logging plugin with  @totalsoft/apollo-logger
  • Loading branch information
alexandra-c committed Jun 17, 2022
2 parents c2697b0 + 2a9f730 commit ac1c706
Show file tree
Hide file tree
Showing 13 changed files with 16,071 additions and 2,278 deletions.
4 changes: 2 additions & 2 deletions .github/workflows/npm-publish.yml
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ jobs:
- uses: actions/checkout@master
- uses: actions/setup-node@v1
with:
node-version: 14.11.0
node-version: 16.x
- run: npm ci
- run: npm test

Expand All @@ -27,7 +27,7 @@ jobs:
fetch-depth: 0
- uses: actions/setup-node@v1
with:
node-version: 14.11.0
node-version: 16.x
registry-url: https://registry.npmjs.org/
scope: '@totalsoft'
- name: Bump version
Expand Down
23 changes: 23 additions & 0 deletions .github/workflows/tests.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
# This workflow will run tests using node and then publish a package to NPM when a release is created
# For more information see: https://help.github.com/actions/language-and-framework-guides/publishing-nodejs-packages

name: run tests

on:
pull_request:
branches: [master]

jobs:
tests:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@master
- uses: actions/setup-node@v1
with:
node-version: 16.x
- name: Install packages
run: npm ci
- name: Test EJS syntax
run: npm run ejslint
- name: Run tests
run: npm test
17 changes: 11 additions & 6 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@

> If you are creating a new web application we recommend you to use our [Webapp Rocket Generator](https://github.com/osstotalsoft/generator-webapp-rocket).
> Also check-out [osstotalsoft/rocket-toolkit](https://github.com/osstotalsoft/rocket-toolkit), a collection of plugins and other GraphQL utilities that may come in handy.
## Installation

First, install [Yeoman](http://yeoman.io) and @totalsoft/generator-graphql-rocket using [npm](https://www.npmjs.com/) (we assume you have pre-installed [NodeJS Latest LTS Version](https://nodejs.org/). Old versions of NodeJS are also supported.)
Expand Down Expand Up @@ -36,7 +37,7 @@ To upgrade an existing project that was scaffold using this **GraphQL Rocket Gen
9. [Multi-tenancy](#multi-tenancy)
10. [Subscriptions](#subscriptions)
11. [Messaging](#messaging)
12. [Error logging](#error-logging)
12. [Logging](#logging)
13. [OpenTracing](#opentracing)
14. [Code examples](#code-examples)
15. [Deployment](#deployment)
Expand Down Expand Up @@ -364,13 +365,17 @@ When using messaging, you need to opt-in between two existing messaging transpor

You can find more details [here](https://github.com/osstotalsoft/nodebb/blob/master/packages/message-bus/README.md#transport)

## Error logging
## Logging

This features includes a logging plugin, that helps you monitor the execution of your GraphQL operations with the use of the request life cycle events. Read more about Apollo Server plugins here: [Plugins](https://www.apollographql.com/docs/apollo-server/integrations/plugins/)
This features uses [@totalsoft/apollo-logger](https://github.com/osstotalsoft/rocket-toolkit/tree/main/packages/apollo-logger#apollo-logger) which comes with an Apollo logging plugin, that helps you monitor the execution of your GraphQL operations with the use of the request life cycle events.

By default all the logs are saved in the application database ( this can be changed by modifying the `saveLogs()` method found in `src/plugins/logging/loggingUtils.js` file.
Read more about Apollo Server plugins here: [Plugins](https://www.apollographql.com/docs/apollo-server/integrations/plugins/)

You need the following table in your database to store your logs:
If desired, the logs can be persisted in a database, file or somewhere else, see [documentation](https://github.com/osstotalsoft/rocket-toolkit/tree/main/packages/apollo-logger#--apollologgingoptions) for more details.

This generator whishes to provide an example of `saveLogs` implementation that can be found in `utils/logging.js` file.

Assuming that by default all the logs are saved in an SQL Server database, the following table should be created:

```
CREATE TABLE [dbo].[EventLog](
Expand All @@ -386,7 +391,7 @@ CREATE TABLE [dbo].[EventLog](
) ON [PRIMARY] TEXTIMAGE_ON [PRIMARY]
```

In addition, the errors thrown inside Apollo Server, are wrapped in a 'user friendly message'. This helps you not to leak sensitive data from unauthorized users.
In addition, the errors thrown inside Apollo Server, are wrapped in a 'user friendly message'. This increases the security of your application by making sure there are no sensitive data leaks.

## OpenTracing

Expand Down
3 changes: 1 addition & 2 deletions __tests__/app.js
Original file line number Diff line number Diff line change
Expand Up @@ -96,8 +96,7 @@ describe('generator-graphql-rocket:app', () => {
})
.run()
.then(() => {
const root = `${tempRoot}/${projectName}/src/plugins/logging`
assert.file([`${root}/loggingPlugin.js`, `${root}/loggingUtils.js`])
assert.fileContent(path.join(__dirname, `${tempRoot}/${projectName}/package.json`), `"@totalsoft/apollo-logger": `)
}))

it('Permissions and rights are ready to be used', () => helpers
Expand Down
2 changes: 1 addition & 1 deletion generators/app/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -83,7 +83,7 @@ module.exports = class extends Generator {
ignoreFiles
)
if (!addTracing) ignoreFiles = concat(['**/tracing/**', '**/__mocks__/opentracing.js'], ignoreFiles)
if (!addGqlLogging) ignoreFiles = concat(['**/plugins/logging/**'], ignoreFiles)
if (!addGqlLogging) ignoreFiles = concat(['**/utils/logging.js'], ignoreFiles)
if (!withRights)
ignoreFiles = concat(
['**/middleware/permissions/**', '**/constants/permissions.js', '**/constants/identityUserRoles.js'],
Expand Down
60 changes: 30 additions & 30 deletions generators/app/templates/infrastructure/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -25,13 +25,13 @@
"author": "",
"license": "ISC",
"dependencies": {
"@graphql-tools/graphql-file-loader": "^7.3.4",
"@graphql-tools/load": "^7.5.2",
"@graphql-tools/schema": "^8.3.2",
"@graphql-tools/graphql-file-loader": "^7.3.15",
"@graphql-tools/load": "^7.5.14",
"@graphql-tools/schema": "^8.3.14",
"@koa/router": "^10.1.1",
<%_ if(dataLayer === "prisma") {_%>
"@prisma/client": "^3.10.0",
"prisma": "^3.10.0",
"@prisma/client": "^3.15.2",
"prisma": "^3.15.2",
<%_ if(withMultiTenancy) {_%>
"lodash.set": "^4.3.2",
<%_} _%>
Expand All @@ -40,65 +40,65 @@
"object-path": "^0.11.8",
"koa-ignore": "^1.0.1",
<%_ if(addQuickStart) {_%>
"datasource-sql": "^1.6.0",
"datasource-sql": "^2.0.1",
<%_}_%>
<%_}_%>
<%_ if(dataLayer === "knex") {_%>
"dataloader": "^2.0.0",
"dataloader": "^2.1.0",
"knex-tiny-logger": "^2.1.0",
"mssql": "^7.3.0",
"mssql": "^8.1.2",
"knex": "^0.95.0",
<%_ if(withMultiTenancy) {_%>
"@totalsoft/knex-filters": "^2.4.0",
"@totalsoft/knex-filters": "^2.4.1",
<%_} _%>
<%_}_%>
<%_ if(addMessaging || (withMultiTenancy && addSubscriptions)) {_%>
"@totalsoft/message-bus": "^2.3.0",
"@totalsoft/messaging-host": "^2.3.0",
"@totalsoft/message-bus": "^2.4.2",
"@totalsoft/messaging-host": "^2.4.2",
<%_}_%>
"apollo-datasource": "^3.3.1",
"apollo-datasource-rest": "^3.5.1",
"apollo-server": "3.6.2",
"apollo-server-koa": "3.6.2",
<%_ if(addGqlLogging) {_%>
"@totalsoft/apollo-logger": "^1.0.6",
<%_}_%>
"apollo-datasource": "^3.3.2",
"apollo-datasource-rest": "^3.6.1",
"apollo-server": "^3.9.0",
"apollo-server-koa": "^3.9.0",
"async-mutex": "^0.3.2",
"bluebird": "3.7.2",
"bufferutil": "^4.0.6",
"colors": "^1.4.0",
"console-stamp": "^3.0.3",
"dotenv": "14.2.0",
"console-stamp": "^3.0.6",
"dotenv": "16.0.1",
"get-stream": "^6.0.1",
"graphql": "15.8.0",
"graphql-middleware": "^6.1.13",
"graphql-middleware": "^6.1.29",
<%_ if(addSubscriptions){ _%>
"apollo-server-core": "^3.6.7",
"apollo-server-core": "^3.9.0",
"graphql-redis-subscriptions": "^2.4.2",
"graphql-subscriptions": "^2.0.0",
"graphql-ws": "^5.6.4",
"ioredis": "^4.28.3",
"ws": "^8.5.0",
"graphql-ws": "^5.9.0",
"ioredis": "^5.0.6",
"ws": "^8.8.0",
<%_}_%>
"graphql-shield": "^7.5.0",
"graphql-tools": "^8.2.0",
"graphql-upload": "^13.0.0",
"humps": "^2.0.1",
<%_ if(addTracing){ _%>
"jaeger-client": "^3.19.0",
"opentracing": "^0.14.7",
"apollo-opentracing": "^2.1.72",
"apollo-opentracing": "^2.1.76",
<%_}_%>
"jsonwebtoken": "8.5.1",
"jwks-rsa": "^2.0.5",
"jwks-rsa": "^2.1.4",
"koa": "^2.13.4",
"koa-bodyparser": "^4.3.0",
"koa-jwt": "^4.0.3",
"lodash.merge": "^4.6.2",
"mime-types": "^2.1.34",
"node-nats-streaming": "0.3.2",
"mime-types": "^2.1.35",
"path": "^0.12.7",
"pino": "^7.6.3",
"ramda": "^0.28.0",
"tedious": "^14.0.0",
"utf-8-validate": "^5.0.8",
"tedious": "^14.6.0",
"utf-8-validate": "^5.0.9",
"utf8-bytes": "0.0.1",
"uuid": "^8.3.2"
},
Expand Down
16 changes: 6 additions & 10 deletions generators/app/templates/infrastructure/src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -20,14 +20,13 @@ const bodyParser = require("koa-bodyparser");

<%_ if(addMessaging) {_%>
// Messaging
const { msgHandlers, middleware } = require("./messaging")
const { msgHandlers <% if(dataLayer == "knex" || addTracing || withMultiTenancy) {%>, middleware <%}%> } = require("./messaging")
const { messagingHost, exceptionHandling, correlation, dispatcher } = require("@totalsoft/messaging-host")
<%_}_%>
<%_ if(addGqlLogging) {_%>
// Logging
const { initializeDbLogging } = require('./plugins/logging/loggingUtils');
const loggingPlugin = require('./plugins/logging/loggingPlugin')
const { v4 } = require('uuid');
const { ApolloLoggerPlugin, initializeLogger } = require('@totalsoft/apollo-logger')
const { saveLogs } = require('./utils/logging')
<%_}_%>
<%_ if(addTracing){ _%>
// Tracing
Expand Down Expand Up @@ -102,7 +101,7 @@ const plugins = [
},
<%_}_%>
<%_ if(addGqlLogging) {_%>
loggingPlugin,
new ApolloLoggerPlugin({ persistLogs: true, persistLogsFn: saveLogs, securedMessages: true }),
<%_}_%>
<%_ if(addTracing){ _%>
tracingEnabled ? tracingPlugin(getApolloTracerPluginConfig(defaultTracer)) : {}
Expand Down Expand Up @@ -155,10 +154,7 @@ const subscriptionServer = useServer(
<%_}_%>
const dataSources = getDataSources()
<%_ if(addGqlLogging){ _%>
const { logInfo, logDebug, logError } = initializeDbLogging(
{ dbInstance, requestId: v4() },
msg?.payload?.operationName
);
const { logInfo, logDebug, logError } = initializeLogger(<% if(dataLayer == "knex") {%>{...ctx, dbInstance}<%} else {%> ctx <%}%>, msg?.payload?.operationName, true, saveLogs)
<%_}_%>

return {
Expand Down Expand Up @@ -194,7 +190,7 @@ const server = new ApolloServer({
context: async ({ ctx }) => {
const { token, <% if(withMultiTenancy){ %>tenantId, externalTenantId, <%}%><% if(dataLayer == "knex") {%>dbInstance,<%}%> externalUser, correlationId, request, requestSpan } = ctx;
<%_ if(addGqlLogging) {_%>
const { logInfo, logDebug, logError } = initializeDbLogging({ ...ctx, requestId: v4() }, request.operationName)
const { logInfo, logDebug, logError } = initializeLogger(<% if(dataLayer == "knex") {%>{...ctx, dbInstance}<%} else {%> ctx <%}%>, request?.body?.operationName, true, saveLogs)
<%_}_%>
return {
token,
Expand Down

This file was deleted.

Loading

0 comments on commit ac1c706

Please sign in to comment.