How to include additional winston log args in JSON output - json

I recently updated the logging in my application, passing an additional parameter with the expectation that the JSON formatted will include it in the log output. However, the additional parameter does not appear in the log file.
When I log this:
logging.http('request', `{"verb":"${verb}", "url": ${url}}`);
the output is this:
{"label":"restClient.ts","level":"http","message":"request","timestamp":"8:20:37.515"}
I have found answers on how to include extra parameters in string row output. But not how to include it within the Winston JSON formatter output.
I'm using the following transport & format configuration:
new DailyRotateFile({
level: 'http',
filename: 'fusionSyncLog_%DATE%.log',
zippedArchive: true,
datePattern: 'YYYY-MM-DD',
format: winston.format.combine(
winston.format.timestamp({ format: timezoned }),
winston.format.label({ label: label }),
winston.format.json()
),
});

Related

How to generate a Swagger #definition from sample JSON

Take the following #definition from the pet store example. Given a #definition section a JSON structure can be generated
e.g.
Is there something that can do the reverse given a largeish complex JSON file?
Given the below JSON Structure can I get the #defintion section of a swagger file generated to save some typing
{
"variable": "sample",
"object1": {
"obj-field1": "field 1 of object",
"obj-field2": "field 2 of object",
"anArray": [
"Value 1",
{
"anArrayObj1": "obj1fieldinarray",
"anArrayObj2": "obj2fieldinarray"
}
]
}
}
You can use this JSON-to-OpenAPI schema converter:
https://roger13.github.io/SwagDefGen/
(GitHub project)
I haven't used it personally though, so I'm not sure how good it is.
Since OpenAPI uses a subset of JSON Schema, you could also use one of the JSON Schema generators, however you may need to manually tweak the generated definition to make it OpenAPI-compatible.
1 - Paste a response in http://www.mocky.io and get a link to your response
2 - Go to https://inspector.swagger.io/ and make a call to your example response
3 - Select the call from "History" and click "Create API definition"
4 - The swagger definition will be available at https://app.swaggerhub.com/
You can use mock-to-openapi cli tool that generates OpenAPI YAML files from JSON mock.
npm install --global mock-to-openapi
then run the conversion of all *.json files from the folder:
mock-to-openapi ./folder/*.json`
Let's have, for example, json object with:
{
"title": "This is title",
"author": "Roman Ožana",
"content" : "This is just an example",
"date": "2020-05-12T23:50:21.817Z"
}
Tool mock-to-openapi converts JSON to the OpenAPI specification as follows:
type: object
properties:
title:
type: string
example: This is title
author:
type: string
example: Roman Ožana
content:
type: string
example: This is just an example
date:
type: string
format: date-time
example: 2020-05-12T23:50:21.817Z
This works for me:
Generate Swagger REST-client code (and POJO) from sample JSON:
Go to apistudio.io:
Insert -> New Model.
CutNpaste your JSON.
[The Swagger YML file will be generated]
Download -> YAML.
Go to editor.swagger.io:
CutNpaste the YML saved from last step.
Generate Client -> jaxrs-cxf-client (there are many other options).

How to get JSON output with Coffeescript?

I have integrated Hubot with elasticsearch and slack.
When we are querying on the API, we are getting the output in JSON format when we use postman.
When we are querying from slack with coffeescript,we are getting plain output.
Here is the code
showHealth = (msg) ->
msg.send("Getting the health for the cluster: ")
msg.http("http://show-acc.com/_cluster/health/")
.get() (err, res, body) ->
lines = body.split("\n")
header = lines.shift()
list = [header].concat(lines.sort().reverse()).join("\n")
msg.send("/code \n ```#{list}```")
This is printing me plain output in slack.
Could you please anyone help me how to change the code to print the output as JSON format?
I believe you need to specify "mrkdwn": true, which will allow you to use backticks for code blocks. However, IMO the nicest way to achieve formatted messages is using Attachments, the structure of which is an array with hashed properties...
I've also got more mileage using robot.messageRoom rather than msg.send, something like this:
# Create attachment
msg = {
attachments: [
{
fallback: 'Getting the health for the cluster: http://show-acc.com/_cluster/health/'
title: 'Getting the health for the cluster:'
title_link: 'http://show-acc.com/_cluster/health/'
text: '/code \n ```#{list}```'
mrkdwn_in: ['text']
}
]
}
# Assign channel
channel = process.env.NOTIFY_ROOM
# Send it!
robot.messageRoom channel, msg
See the following refs for further info:
https://api.slack.com/docs/message-formatting#message_formatting
https://api.slack.com/docs/message-attachments

How to add html reporting to a cucumber/chimp e2e test

I am using the example provided on the chimp website for gulp-chimp
gulp.task('chimp-options', () => {
return chimp({
features: './features',
browser: 'phantomjs',
singleRun: true,
debug: false,
output: {
screenshotsPath: './screenshots',
jsonOutput: './cucumber.json',
},
htmlReport: {
enable: true,
jsonFile: './e2e_output/cucumber.json',
output: './e2e_output/report/cucumber.html',
reportSuiteAsScenarios: true,
launchReport: true,
}
});
});
the problem i have and that it's killing me is that when I run gulp chimp-options i get :
Unable to parse cucumberjs output into json: './e2e_output/cucumber.json' SyntaxError: ./e2e_output/cucumber.json: Unexpected end of JSON input
What am I doing wrong ?
I believe chimp is just a wrapper on multiple frameworks/libraries out there and I'm pretty sure they just use cucumber-html-reporter to generate its HTML reports.
If you still can't get it working automatically via chimp, just generate the options file as usual and npm install cucumber-html-reporter and then use it to generate the same report.
Create a separate file called generate_html_report.js and paste in the code under Usage. Then add this to your npm script to run after your test suite has finished. I'd avoid putting it in your afterHooks as I've had issues in the past where the JSON file hadn't been completely generated before it tries to run the script expecting the JSON file to be there.

Generating JSON object of the tests ran using Protractor?

I'm relatively new to end to end testing with Protractor, Mocha and Yadda (for integration with Mocha so I can use Gherkin and step definitions).
I've seen a plugin called Mochawesome which generates an HTML report which can be viewed offline, along with this JSON object of the test results, all of which contained in a 'reports' folder.
I presume it's Mochawesome which generates these JSON objects as the HTML page seems to have corresponding tags etc. Is there any way to generate a JSON object of the tests ran without the HTML reporter? The idea was to create my own sort of 'dashboard' containing information about the tests based on the JSON information.
Yes you can create a JSON report of your specs/tests with protractor.You just have to put resultJsonOutputFile: './Report.json' in your config file.
your config file should somewhat look like this:
exports.config = {
directConnect: true,
capabilities: {
'browserName': 'chrome'
},
baseUrl: 'http://juliemr.github.io/protractor-demo/',
framework: 'jasmine2',
specs: ['*spec.js '],
allScriptsTimeout: 180000,
getPageTimeout: 180000,
jasmineNodeOpts: {
defaultTimeoutInterval: 180000
},
resultJsonOutputFile: './Report.json', // It would create report.json file in your current folder
onPrepare: function () {
browser.driver.manage().window().maximize();
browser.ignoreSynchronization = true;
}
};
You can consume this json report and use it in your way!
I'm not sure about generating JSON object directly in protractor. But what i know is that, we can generate results in XML and then convert xml to json by writing some customized code.
Code for generating XML reports are as follows:
framework: "jasmine2",
onPrepare: function() {
var jasmineReporters = require('jasmine-reporters'),
jasmine.getEnv().addReporter(new jasmineReporters.JUnitXmlReporter({
savePath: '../result/',
filePrefix: ‘report’,
consolidateAll: true
});
);
},

response with status 200: ok

I am trying to figure out how to plot data from a local '.JSON' file using angular2-highcharts example.
I followed the example in 'https://www.npmjs.com/package/angular2-highcharts' to first understand how to plot .JSON data and it worked. I took the data available for the example and created a local .JSON file (copied the content from 'https://www.highcharts.com/samples/data/jsonp.php?filename=aapl-c.json&callback=JSONP_CALLBACK' in notepad and saved it with UTF-8 encoding as a .JSON file), and replaced the file path for the JSON request to this. When I do this though, I get an error - response with status 200.
constructor(jsonp : Jsonp) {
//jsonp.get('https://www.highcharts.com/samples/data/jsonp.php?filename=aapl-c.json&callback=JSONP_CALLBACK').subscribe(res => {
jsonp.get('./data.json').subscribe(res => {
this.options = {
title : { text : 'AAPL Stock Price' },
series : [{
name : 'AAPL',
data : res.json(),
tooltip: {
valueDecimals: 2
}
}]
};
});
}
options: Object;
};
Since I am not super familiar with json data/ Javascript or angular2 I am not sure if I am missing something very basic here. Any help is appreciated.
as far as I know, Response Status 200 specifies that request was successful. i.e. your request was successfully handled. perhaps you want to try checking response data.
check your callback for response data.
Using http instead of json helped. I made use of the suggestion in this answer https://stackoverflow.com/a/36305814/4567096.