I'm working on a new project using Ionic (AngularJs-based) and everything is working as it should be.
I'm currently outputting every 'Function call' (every step) to the console (via a Debug-Function) for debugging purposes so that I can quickly see if every function gets called correctly.
Log Function (minimised/simplified code):
.factory('DebugMode', ['$log', function($log) {
var DebugMode = {};
this.active = true;
this.console = function(LogLine, LogStyle)}
if(DebugMode.active){
$log.log(LogLine);
}
};
return DebugMode;
My question:
Is there a way or a method that I can also write that output (not specific the console output, but the logs I write to it) to a file or variable?
My purpose: Able to request the log when I'm running the app stand-alone and don't have access to a browser or some sort. So that I can easily see if everything is still running correctly or if a client is experiencing some issues, I can ask them to send the Log via mail or some sort.
For ex. (in the app): Open the menu, go to settings, show log, see where the app returned an error and/or press a button to send that log to me.
Would the best way be to write everything to a 'log-output.date().txt' sort of thing or store everything in a session variable that gets cleared on exit?
Thanks in advance!
-Bert
After some research, I think FileLogger would be the best solution and does exactly what I had in mind (more or less).
Related
I am running a bit of a complicated setup where several files interact with each other:
a python file, setting up a server, which is used to connect two users online
a combination of js and html files to set up the web page that each user interacts with
So each user interacts with the js files, which in turn send a message to the python file, which reacts by sending the appropriate response to the js files on the other user’s end, etc.
To launch all this, I simply open the python file in my terminal — thus opening up the websockets — and then I type in the address of my html file on my browser. I know the functions in my python file are executing correctly because the interaction does work on my browser, however, none of the prints in the functions show up on my terminal…
So for example, in the python file:
def message_received(client, server, message):
print("Client(%d) said: %s" % (client['id'], message))
response = json.loads(message)
response_code = response['response_type']
handle_client_response(client['id'], response_code, response)# another function defined elsewhere
PORT = 9004
print('starting up')
server = WebsocketServer(PORT, '0.0.0.0')# this is calling the actual server set up from another file, which I didn 't write myself.
server.set_fn_message_received(message_received)
server.run_forever()
The "starting up" is the only thing that will actually print, the print in message_received doesn't show up, even though I know for a fact the function is working because handle_client_response is called correctly.
My guess is this is because the function are not actually executed on the terminal, but on the server that I set up, so python is trying to print in the server instead of the terminal. But also I have no idea what I’m talking about — first time I ever do this type of complicated files interaction so I’m very confused!
Am I guessing the problem correctly? Any fix for it?
Try maybe using a library like logging to handle the print of messages. That way, you can specify where the log messages should be written to.
import logging
logging.basicConfig(level=logging.DEBUG)
def message_received(client, server, message):
logging.debug("Client(%d) said: %s" % (client['id'], message))
response = json.loads(message)
response_code = response['response_type']
handle_client_response(client['id'], response_code, response)
Hopefully this will fix it.
so I'm trying to host my website on Heroku and set up everything to get my app up and running.
Whenever I try to submit the form I get undefined errors.
Undefined Errors Console Errors
I've set it up to use the port like shown in the documenation:
app.listen(process.env.PORT || 8081, function () {
console.log('Example app listening on port 8081!');
});
When starting the app locally with heroku local web I get Typerror: Failed to Fetch and the undefined results but when I go into my .env file and add a port=8081 it works perfectly fine.
Good result
When I open it with heroku open I still have that undefined problem.
I don't really have to set a PORT in .env right? Or do I?
I read that that standard port is 80 but that didn't work either.
Can someone please help me?
Thank you!
Heres the link to the public site: https://shrouded-everglades-61993.herokuapp.com/
Here the link to my Github rep: https://github.com/stefanfeldner/capstone-travel-app
So the reason that they're undefined is that they are being set by these lines in main.js:
uiData.imageURL = data[1].imageUrl;
...
uiData.iconCode = data[0].iconCode;
Where data is the object you're retrieving from your /getData endpoint. The problem is that what /getData actually returns is [{}, {}] so of course these values are both undefined, leading to that visual broken-ness.
Now, why does /getData return these empty objects? I can't check your server logs, but there are two obvious possibilities based on the way server.js is written.
The first is that there's an error somewhere and you're simply not making it all the way to the end of your try-catch in callApi, so neither weatherData nor pixabayData are being updated.
Second, it's also possible that these calls are successful but that the desired data is not in the results, i.e. that neither of these if statements are true:
if('city_name' in data) {`
...
if('hits' in data) {
Again, in this case, neither weatherData nor pixabayData are being updated.
The way that your handler for /sendFormData is written, it doesn't check that callApi actually got any useful data, just that it has finished execution. So your code flow continues on its merry way despite the data objects still being empty.
However, there's a bigger, unrelated design flaw here: What happens if more than one person uses your website? Your client-side code calls /sendFormData, which hopefully correctly populates the global variables weatherData and pixabayData, and then separately calls /getData to try and retrieve this data.
The thing is, though, between the time your client-side calls /sendFormData and /getData, anyone else using your website could separately call /sendFormData and change the data contained in the global variables from the result of your search to the result of their search. So you'd get their search results back instead of yours, since their results overwrote your results on the server. You need to handle getting the API results and sending them back to the requester in a single transaction.
(Re: all the local Heroku Config, that's hard to answer without messing around with your local computer, sorry.)
Tech stack: Angular 7, Spring webflux (with spring boot), Chrome browser
I cannot share the actual code due to policy restrictions. Appreciate your understanding.
I have followed this example: https://medium.com/#chrisbautistaaa/server-sent-events-in-angular-node-908830cc29aa
I have a boolean variable makeCall acting as a condition switch for my if else
When user clicks a button on screen toggleSseCall method containing the below logic is called in Angular component
Body of toggleSseCall method:
makeCall: boolean = true;
toggleSseCall() {
let eventSource = new EventSource(url);
if(makeCall) {
Call service method: getServerSentEvents(eventSource) //similar to the example in link one difference being i'm passing the Instantiated eventSource reference
Subscribe to the observable returned by the above call like in the example and console log the data
} else { //When user clicks the button again, call goes here as makeCall is false
//Call flow comes here as expected, I verified that with console logging
eventSource.close(); //I verified to make sure the close method is not uppercase or anything like that
}
makeCall = !makeCall //toggle boolean flag
}
Server side:
Just RESTFul Get Api call that returns Flux and the Flux just returns hello string every x seconds
The call is happening and the "hello" text streams from back-end to front-end as expected. But, the data stream doesn’t stop on call to close method. I want the data stream to close when user clicks the button again. Would appreciate any suggestions on this.
A bit late, but I just came across this issue myself. For me, this only happens when developing locally via ng serve, using the Webpack dev server to proxy requests to my backend app.
The proxy is implemented using the NPM package http-proxy-middleware. There's
an unresolved issue suggesting this is a bug.
When the browser connects directly to the backend API, this issue doesn't happen for me.
I've been trying to figure out a way to use notifications on a background process and couldnt find anything online about it. So, I figured out one way around it and wanted to share (Not sure if this is the best way to go about doing this but here goes:)
Problem: I want to notify the user of new info when the page is running but in the background (blurred). I could use alert('new info!'); to get the taskbar icon to flash, but then you have to manually dismiss it (tried it and it's hella annoying). I really liked the notifications, but they only work if the user performs an action, so not helpful...
I hope I won't be telling something stupid, but from where I see it (and remember from school) that's basically how http works : a request is sent to the server, which issues a response eventually after executing some server-side code.
Basically you're asking for a "PUSH" functionality from server to client, and in that case you can't make use of HTTP.
Some tricks exist to work around this limitation, but basically they're all issuing requests at a certain frequency (Dave's answer does exactly that). If your site doesn't change that much, that means a lot of requests are issued for no reason (nothing has changed), consuming bandwith for nothing.
From what I know, the answer to this is called Websockets, which are supported by recent browsers only. I never had the chance to use it though so I couldn't tell much more about it. This allows full duplex communication, thus allowing server to "push" data to the client. I guess that's what SO uses for "new message" notifications (top left of the screen - you see immediately when you receive a new message)
My solution: I made a chrome extension that runs in the background and triggers the notifications. It's a little limited in scope as you need to have chrome to do it, but it does what i need it to, and for the purposes of the problem i'm working on, i can just make my user group use chrome ;D
The specifics: The extension only has two components, the manifest and a script. Currently, i setup the manifest so that it only works on my site using the match identifier... and i set the permissions to include notifications.
The JS script has a window.setinterval that looks for an element in the page with the id NOTIFIER. If it's empty, it does nothing, otherwise it creates a notification based on the content and then clears the content to prevent showing the same notification multiple times... (I tried using .onchange for that element, but couldn't get the event to trigger... I'd prefer to do this on an event rather then setInterval)
Notify.js
function onExtLoad() {
var timer = setInterval(refresh,1000);
}
document.addEventListener('DOMContentLoaded', onExtLoad());
function refresh() {
if (document.getElementById('NOTIFIER').innerHTML == "") {
//do nothing?
} else {
var notification = webkitNotifications.createNotification("",
"You got a new message",
document.getElementById('NOTIFIER').innerHTML);
notification.show();
document.getElementById('NOTIFIER').innerHTML = "";
}
}
Then, all i need to do is have the JS on the page control when it adds info the the NOTIFIER and voila! notifications!
Hope this helps someone else.
#ExpertSystem: I messed around with the MutationObserver but I can only get it to trigger once. Here's a JSFiddle: http://jsfiddle.net/BTX8x/1/
Am I missing something? Is there a way to reset it?
EDIT: Figured it out, i needed subtree:true
I have a simple app written in meteor here: https://gist.github.com/drew-gross/6202629 that is having problems. When the Template.tab.ordered_drinks function is called, no Tabs are found (Tabs.find({}).fetch() returns an empty array) but only most of the time. Sometimes the Tabs are there. I have checked that they exist in the database.
To reproduce:
1) Run the app. (requires meteor-router)
2) Create a tab on the console: Tabs.insert({owner:"foo"})
3) Try to view the page for the tab: localhost:3000/tabs/:id
The page will work or not work, seemingly at random. To confirm, you can set a breakpoint on line 9 or 19 of the .js and do Tabs.find({}).fetch() and you will sometimes see and sometimes not see the tab.
This kinda seems like a bug in Meteor but I thought I would check first to see if I might be doing something wrong.
Browser: Chrome 28
Meteor version: Release 0.6.4.1
Node version: v0.10.15
It looks like the issue might arise if the subscriptions haven't yet got their data.
In meteor the html/js is sent down to the client, it then renders the page. At the same time the client connects to the server via websockets/long polling to retrieve the mongo database.
So sometimes you can render html without yet having anything in your database.
You just need to be careful that you use reactivity to handle when the data arrives and not presume its already there, like here:
Template.tab.ordered_drinks = function () {
return Tabs.findOne(Session.get('tabId')).ordered_drinks;
};
if Tabs.findOne(Session.get('tabId')) is null because there isn't any data on the client (yet) then .ordered_drinks wont exist and you would get an error on the chrome console & sometimes see/sometimes not see the tab, depending on whether the database has loaded yet or not.
Try altering the line below to allow Tabs.findOne(Session.get('tabId')) to be null without throwing an error.
Template.tab.ordered_drinks = function () {
var tab = Tabs.findOne(Session.get('tabId'));
return tab && tab.ordered_drinks;
};
So at this point it is loading. As soon as the data arrives it'll reactively re-render this portion with the tabs.