Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
  • Loading branch information
liam-grace committed Mar 16, 2017
2 parents eae4642 + 30dba12 commit 2e41b59
Show file tree
Hide file tree
Showing 66 changed files with 848 additions and 252 deletions.
12 changes: 7 additions & 5 deletions .travis/deploy.sh
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ fi

# are we building the docs?
if [ "${DOCS}" != "" ]; then
if [ -z "${TRAVIS_TAG}" ]; then
if [ -z "${TRAVIS_TAG}" ]; then
DOCS="full"
else
DOCS="unstable"
Expand All @@ -51,8 +51,8 @@ npm config set registry https://registry.npmjs.org/
npm config set //registry.npmjs.org/:_authToken ${NPM_TOKEN}

# Set the GitHub deploy key we will use to publish.
set-up-ssh --key "$encrypted_568b95f14ac3_key" \
--iv "$encrypted_568b95f14ac3_iv" \
set-up-ssh --key "$encrypted_8496d53a6fac_key" \
--iv "$encrypted_8496d53a6fac_iv" \
--path-encrypted-key ".travis/github_deploy_key.enc"

# Change from HTTPS to SSH.
Expand Down Expand Up @@ -103,6 +103,7 @@ if [ -z "${TRAVIS_TAG}" ]; then
cf push fabric-composer-next-unstable -c "node cli.js" -i 2 -m 128M --no-start
cf set-env fabric-composer-next-unstable CLIENT_ID ${GH_NEXT_UNSTABLE_OAUTH_CLIENT_ID}
cf set-env fabric-composer-next-unstable CLIENT_SECRET ${GH_NEXT_UNSTABLE_OAUTH_CLIENT_SECRET}
cf set-env fabric-composer-next-unstable USABILLA_ID ${USABILLA_ID}
cf start fabric-composer-next-unstable
popd

Expand Down Expand Up @@ -143,12 +144,13 @@ else
cf push fabric-composer-next -c "node cli.js" -i 2 -m 128M --no-start
cf set-env fabric-composer-next CLIENT_ID ${GH_NEXT_OAUTH_CLIENT_ID}
cf set-env fabric-composer-next CLIENT_SECRET ${GH_NEXT_OAUTH_CLIENT_SECRET}
cf set-env fabric-composer-next USABILLA_ID ${USABILLA_ID}
cf start fabric-composer-next
popd

# Configure the Git repository and clean any untracked and unignored build files.
git config user.name "Travis CI"
git config user.email "[email protected]"
git config user.name "${GH_USER_NAME}"
git config user.email "${GH_USER_EMAIL}"
git checkout -b master
git reset --hard
git clean -d -f
Expand Down
16 changes: 8 additions & 8 deletions .travis/deploy_docs.sh
Original file line number Diff line number Diff line change
Expand Up @@ -8,14 +8,14 @@ set -o pipefail
DIR="$( cd "$( dirname "${BASH_SOURCE[0]}" )/.." && pwd )"
date
# Set the GitHub deploy key we will use to publish.
set-up-ssh --key "$encrypted_f19708b15817_key" \
--iv "$encrypted_f19708b15817_iv" \
--path-encrypted-key ".travis/github_deploy_docs_key.enc"
set-up-ssh --key "$encrypted_8496d53a6fac_key" \
--iv "$encrypted_8496d53a6fac_iv" \
--path-encrypted-key ".travis/github_deploy_key.enc"

# push the html documents
# Configure the Git repository and clean any untracked and unignored build files.
git config user.name "Travis CI"
git config user.email "[email protected]"
git config user.name "${GH_USER_NAME}"
git config user.email "${GH_USER_EMAIL}"
git config push.default simple

echo ${DIR}
Expand All @@ -27,15 +27,15 @@ git clone [email protected]:fabric-composer/${REPO}.git
git remote set-url origin ${REPO}.git

cd "${DIR}/packages/composer-website/out/${REPO}"

if [ "${DOCS}" == "full" ]; then
rm -rf ${DIR}/packages/composer-website/out/${REPO}/*
cp -rf ${DIR}/packages/composer-website/jekylldocs/_site/* .
cp -rf ${DIR}/packages/composer-website/jekylldocs/_site/* .
fi

mkdir -p ${DIR}/packages/composer-website/out/${REPO}/unstable
rm -rf ${DIR}/packages/composer-website/out/${REPO}/unstable/*
cp -rf ${DIR}/packages/composer-website/jekylldocs/_site/* ./unstable
cp -rf ${DIR}/packages/composer-website/jekylldocs/_site/* ./unstable

git add .

Expand Down
Binary file removed .travis/github_deploy_docs_key.enc
Binary file not shown.
Binary file modified .travis/github_deploy_key.enc
Binary file not shown.
206 changes: 206 additions & 0 deletions contrib-notes/diagnostic-logging.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,206 @@
# Diagnostic logging
Fabric-composer has a functional logger that can be used for both informational and diagnostic log messages. It also permits customization for different logging 'back-ends'.

## Log points
Log points are added throughout the codebase, and should be very similar to other logging systems. The following APIs match to the standard log levels of *debug, verbose, info, warn, error* Note that *silly* isnt' being used.

```javascript
debug(method, msg, data);
verbose(method, msg, data);
info(method, msg, data);
warn(method, msg, data);
error(method, msg, data);
```

In addition there are `entry` and `exit` APIs for use to indicate the entry and exit of functions - these are logged at the *debug* level.

```javascript
entry(method, data);
entry(method, data);
```

These methods are called on a `logger` object. To obtain one of these the following code at the top of the file should be used.

```javascript
\\ For the businessnetworkdefinition.js file in the composer-common module
const LOG = Logger.getLog('common/BusinessNetworkDefinition');
```

## Usage within the code.

Taking the `businessnetworkdefinition.js` file as an example, these are some real log statements:

```javascript
constructor(identifier, description, packageJson, readme) {
const method = 'constructor';
LOG.entry(method, identifier, description);

// ----
LOG.info(method, 'Created package.json' + JSON.stringify(packageJson));
// ----
LOG.debug(method, 'Found model file, loading it', file.name);
// ----

LOG.exit(method);
}
```

All the log APIs can take a variable number of data arguments for logging. Any *error* object is logged will have it's stack trace located.

## Enabling the logging

In commong with other node.js applications the `DEBUG` environment variable is used. This takes a comma separated list of the modules that need to be logged.

Examples

- `DEBUG=*` Logs everything from everything (not just Fabric-Composer)
- `DEBUG=composer:*` Logs everything from just Fabric-Composer
- `DEBUG=*,!composer:*` Logs everything from everything with the exception of Fabric-Composer
- `DEBUG=composer:common` Logs everything from the Fabric-Composer common module (the composer-common npm module)
- `DEBUG=composer:client,composer:common` Logs everything from the Fabric-Composer common module (the composer-common npm module), and the client module
- `DEBUG=composer:common:businessnetworkdefinition` Logs the businessnetworkdefinition ONLY

## Controling the level and output

The structure of the Fabric-Composer log code is that it delegates the actually logging to a back-end service. This service can swapped by using configuration (see below) but by default uses the Winston library.

### Default configuration
There are two streams setup in the default configuration - one to write log events to a file, the other to the console.

If log is not enabled for Fabric-Composer no events are sent to the Console but info events are sent to the file
If the log is enabled then info events are sent to the console and all level of events are sent to the file.

The file by default is written to a directory off the current working directory called `logs` with the name `trace_<processid>.log`

### Configuring the default logger

Configuration is handled by using the config package - a config file called default.json is unless the code has specified something else.
As an example - the default configuration of the logger would be represented in this file as

```
$ cat ./config/default.json
{
"fabric-composer": {
"debug": {
"logger": "./winstonInjector.js",
"config": {
'console': {
'enabledLevel': 'info',
'alwaysLevel': 'none'
},
'file': {
'filename': 'trace_PID.log',
'enabledLevel': 'silly',
'alwaysLevel': 'info'
}
}
}
}
}
```
### Modyifing the Winston logger
Here are two examples of how to change the back-end logger simply using Winston's changeable transport feature.

Two cloud hosted application log sites are *Loggly.com* and *Papertrailapp.com*

#### Logger class
Create a new js file, eg `winstonPapertrailInjector.js`

```javascript
'use strict';

const fs = require('fs-extra');
const winston = require('winston');
const sprintf = require('sprintf-js').sprintf;

//
// Requiring `winston-papertrail` will expose
// `winston.transports.Papertrail`
//
require('winston-papertrail').Papertrail;

/** The json structure that has been specified in the configuration
* @private
* @param {Object} config JSON structure with specific configuration information
* @param {Array} configElements array with the DEBUG env variables for composer
*
* @returns {Object} object that is the logger to use
*/
exports.getLogger = function (config,configElements){

let consoleLevel;
let logglyLevel;

if (configElements.debug.length === 0){
consoleLevel='error';
logglyLevel='info';
} else {
papertrailLevel=config.papertrail.enabledLevel;
consoleLevel=config.console.enabledLevel;
}

let formatterFn = function(options) {
// Return string will be passed to logger.
return sprintf('%s %-7s %-20s %s'
,options.timestamp()
,options.level.toUpperCase()
,options.message
,(JSON.stringify(options.meta,null,'') +'$')
);

};

let timestampFn = function() {
return new Date(Date.now()).toISOString();
};

// this is the key part to route to Papertrail - the host and port
let newWinstonLogger = {
transports: [
new(winston.transports.Papertrail)( {
name:'papertrail',
host: 'logs5.papertrailapp.com',
port: '34662',
timestamp: timestampFn,
formatter: formatterFn ,
level: papertrailLevel,
json:true
})

]
};

winston.loggers.add('Fabric-Composer',newWinstonLogger);
return winston.loggers.get('Fabric-Composer');


};

```

and in a configuration file - where the logger is a reference to the code above.

```
{
"fabric-composer": {
"debug": {
"logger": "/home/matthew/github/waste-notes/winstonPapertrailInjector.js",
"config": {
"console": {
"enabledLevel": "info",
"alwaysLevel": "none"
},"papertrail": {
"enabledLevel": "silly",
"alwaysLevel": "info"
}
}
}
}
}
```
2 changes: 0 additions & 2 deletions packages/composer-connector-hlfv1/lib/hlfconnection.js
Original file line number Diff line number Diff line change
Expand Up @@ -160,7 +160,6 @@ class HLFConnection extends Connection {
// Set the user object that the client will use.
LOG.debug(method, 'Persisting user context into key value store');
this.user = user;
this.user.mspImpl._id = 'Org1MSP';
return this.client.setUserContext(user);

})
Expand Down Expand Up @@ -216,7 +215,6 @@ class HLFConnection extends Connection {
let result = new HLFSecurityContext(this);
result.setUser(enrollmentID);
this.user = user;
this.user.mspImpl._id = 'Org1MSP';
LOG.exit(method, result);
return result;

Expand Down
4 changes: 2 additions & 2 deletions packages/composer-connector-hlfv1/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -42,8 +42,8 @@
"dependencies": {
"composer-common": "^0.5.2",
"composer-runtime-hlfv1": "^0.5.2",
"fabric-client": "^0.2.1",
"fabric-ca-client": "^0.2.1",
"fabric-client": "0.2.3",
"fabric-ca-client": "0.2.3",
"fs-extra": "^1.0.0",
"semver": "^5.3.0",
"temp": "^0.8.3",
Expand Down
4 changes: 0 additions & 4 deletions packages/composer-connector-hlfv1/test/hlfconnection.js
Original file line number Diff line number Diff line change
Expand Up @@ -54,10 +54,6 @@ describe('HLFConnection', () => {
mockCAClient = sinon.createStubInstance(FabricCAClientImpl);
mockUser = sinon.createStubInstance(User);

// TODO: Temp code to address patch due to node-sdk not having fix yet
mockUser.mspImpl = {};
mockUser.mspImpl._id = '';

mockSecurityContext = sinon.createStubInstance(HLFSecurityContext);
mockBusinessNetwork = sinon.createStubInstance(BusinessNetworkDefinition);
mockBusinessNetwork.getName.returns('org.acme.biznet');
Expand Down
13 changes: 13 additions & 0 deletions packages/composer-playground/cli.js
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,9 @@ Logger.setFunctionalLogger({
});

const app = require('composer-playground-api')(argv.port);
const cheerio = require('cheerio');
const express = require('express');
const fs = require('fs');
const isDocker = require('is-docker');
const opener = require('opener');
const path = require('path');
Expand All @@ -72,6 +74,17 @@ if (process.env.COMPOSER_CONFIG) {
}

const dist = path.resolve(__dirname, 'dist');
if (process.env.USABILLA_ID) {
const indexFile = path.resolve(dist, 'index.html');
const indexHTML = fs.readFileSync(indexFile, 'utf8');
const usabillaTemplateFile = path.resolve(__dirname, 'usabilla.html.template');
const usabillaTemplate = fs.readFileSync(usabillaTemplateFile, 'utf8').replace(/%USABILLA_ID%/, process.env.USABILLA_ID);
const $ = cheerio.load(indexHTML);
$('body').append(usabillaTemplate);
const modifiedIndexHTML = $.html();
fs.writeFileSync(indexFile, modifiedIndexHTML, 'utf8');
}

app.use(express.static(dist));
app.all('/*', (req, res, next) => {
res.sendFile('index.html', { root: dist });
Expand Down
5 changes: 5 additions & 0 deletions packages/composer-playground/config/webpack.common.js
Original file line number Diff line number Diff line change
Expand Up @@ -213,6 +213,11 @@ module.exports = function (options) {
// sourceMap: true
}
}]
},

{
test: /\.bna$/,
loader: "buffer-loader"
}

],
Expand Down
5 changes: 5 additions & 0 deletions packages/composer-playground/config/webpack.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -212,6 +212,11 @@ module.exports = function (options) {
{
test: /browserfs.*\.js$/,
loader: 'imports-loader?importedSetImmediate=setimmediate'
},

{
test: /\.bna$/,
loader: "buffer-loader"
}
]
},
Expand Down
Loading

0 comments on commit 2e41b59

Please sign in to comment.