beansio
TypeScript icon, indicating that this package has built-in type declarations

1.1.33 • Public • Published

Beans (😏)

Logger for logging system events for Beansio

How to use beans to log middleware events

Remix.js Route logger Example

///init beans in the server.tsx file
import { plantBeans, remixRouteLogger,remixBeans } from 'beansio'

plantBeans('APII-KEY')
//init special remix functions

//set up sessions
const sessionStorage = createCookieSessionStorage({
  cookie: {
    name: "__session",
    httpOnly: true,
    maxAge: 0,
    path: "/",
    sameSite: "lax",
    secrets: [process.env.SESSION_SECRET || ''],
    secure: process.env.NODE_ENV === "production",
  },
});

//add middleware for remix
app.use(
    remixRouteLogger(

        async (req: any) => {
            const USER_SESSION_KEY = "userId";

            //this function sets user identifiers that can be stored on the db
            const cookie = req.headers['cookie']
            const session = await sessionStorage.getSession(cookie)
            const user =  session.get(USER_SESSION_KEY)

            return  user; ///optional customise return messages
        }
        , async (req: any) => {
            //this function lets users set custom meta data to be stored
            return {}; //set meta data
        }
    )
)   

Graphql example

import { plantBeans, graphQlLogger } from 'beansio'

plantBeans('APII-KEY')

const server = new GraphQLServer({
  schema,
  context: createContext,
  middlewares: [permissions],
})
server.express.use(

  graphQlLogger((req: any) => {
    //this function sets user identifiers that can be stored on the db
    if (!req.user) {
      return ""; //must be a string
    }
    return req.user.email || ""; ///optional customise return messages
  }, (req: any) => {
    //this function lets users set custom meta data to be stored
    return {}; //set meta data
  })
)
server.start(res => console.log(`We're live on port ${res.port}`));

Express Example

const express = require('express')
const app = express()
const port = 3100
//import beans library
import { plantBeans, routeLogger } from 'beansio'

plantBeans('APII-KEY')

//Calling the ExpressBeans function to log all 
app.use(routeLogger(  (req )=>{
  //this function sets user identifiers that can be stored on the db
 

  if ( !req.user) {
      return ""; //must be a string
  }

   return req.user.email || ""; ///optional customise return messages
} ,(req)=>{
   //this function lets users set custom meta data to be stored
   return  {}; //set meta data
}))




app.get('/', (req, res) => {
  res.send('Hello World!')
})

app.listen(port, () => {
  console.log(`Example app listening on port ${port}`)
})

How to use beans to log Custom messages

    const event = () =>{
        ///perfom operation
        beans.logMessage('Event triggered by user ',{})
    }

How to use beans to log an error event

    const startJokeServer =   () =>{
        app.listen(port, () => {
           //////
        }).on('error', (error) => {
            beans.logError('Error starting server',{})
        });
    }

How to use beans as an error handler

    // Error fallback
    import { plantBeans, beansErrorHandler } from 'beansio'

    plantBeans('APII-KEY')
    ///
    ///
    app.use(beansErrorHandler);
    

beansio

beansio

Versions

Current Tags

VersionDownloads (Last 7 Days)Tag
1.1.3321latest

Version History

VersionDownloads (Last 7 Days)Published
1.1.3321
1.1.320
1.1.313
1.1.300
1.1.290
1.1.280
1.1.270
1.1.260
1.1.250
1.1.230
1.1.220
1.1.210
1.1.200
1.1.190
1.1.180
1.1.170
1.1.160
1.1.150
1.1.140
1.1.130
1.1.120
1.1.110
1.1.10
1.1.00
1.0.9200
1.0.9100
1.0.9000
1.0.900
1.0.80
1.0.70
1.0.60
1.0.50
1.0.40
1.0.30
1.0.20
1.0.10
1.0.00

Package Sidebar

Install

npm i beansio

Weekly Downloads

24

Version

1.1.33

License

none

Unpacked Size

27.3 kB

Total Files

6

Last publish

Collaborators

  • king__somto