php-general Digest 29 Feb 2012 08:29:07 -0000 Issue 7706

Topics (messages 316789 through 316797):

Re: Insert new array after specific key in multidimensional array
        316789 by: Micky Hulse
        316790 by: Adam Richardson
        316791 by: Micky Hulse

How do I enable more useful PHP error logging?
        316792 by: Daevid Vincent
        316793 by: Adam Richardson
        316794 by: Stuart Dallas
        316795 by: Daevid Vincent
        316796 by: Tommy Pham
        316797 by: Simon Schick

Administrivia:

To subscribe to the digest, e-mail:
        php-general-digest-subscr...@lists.php.net

To unsubscribe from the digest, e-mail:
        php-general-digest-unsubscr...@lists.php.net

To post to the list, e-mail:
        php-gene...@lists.php.net


----------------------------------------------------------------------
--- Begin Message ---
On Tue, Feb 28, 2012 at 8:36 AM, Shawn McKenzie <nos...@mckenzies.net> wrote:
> Might be an easier way but this should work.  You can sort the $before =
> true out for yourself :-)

Hi Shawn, I've updated your function to do the $before bit:

<https://gist.github.com/1928452#file_array_insert.php>

I also set it up to merge the new array to the end if the key does not exist.

Out of all the examples I have found, your approach looks the cleanest
and the most compact.

I'm open to feedback.

Many thanks!!!!

Cheers,
M

--- End Message ---
--- Begin Message ---
On Mon, Feb 27, 2012 at 9:12 PM, Micky Hulse <mickyhulse.li...@gmail.com>wrote:

> Howdy!
>
> Example code:
>
> <https://gist.github.com/1928452>
>
> What would be the best way to insert $o_insert array into $o array
> after specified key?
>
> I hate to just ask for example code, but I can't seem to find the
> perfect solution. :(
>
> Many thanks in advance for the help!
>
> Cheers,
> Micky
>
> --
> PHP General Mailing List (http://www.php.net/)
> To unsubscribe, visit: http://www.php.net/unsub.php
>
>
Out of curiosity, why are you worried about the order of elements in an
associative array?

Thanks,

Adam

-- 
Nephtali:  A simple, flexible, fast, and security-focused PHP framework
http://nephtaliproject.com

--- End Message ---
--- Begin Message ---
Hi Adam!

On Tue, Feb 28, 2012 at 2:05 PM, Adam Richardson <simples...@gmail.com> wrote:
>> What would be the best way to insert $o_insert array into $o array
>> after specified key?

Great question. :D

Quick answer: It's a Wordpress thang!

Long answer: I am working with WordPress and the theme that I am using
stores all of its settings in a multidimensional array... The WP admin
displays the contents of the array as a GUI and the order of the array
is what dictates the display order.

--- End Message ---
--- Begin Message ---
My question is, is there a way to enable some PHP configuration that would
output more verbose information, such as a backtrace or the URL attempted?

In our PHP error log, we have the usual semi-useful information. However
this is only a partial story as it's hard to re-create the URL that caused
the error. In the first Actor example, yeah actor_id 2206 doesn't exist and
so now I have put a try/catch on all pages that have "new Actor($actor_id)"
but it doesn't tell me WHY this is happening. How did someone get to this
point? I doubt they just randomly picked '2206' which happens to be one of
only a handful of actually missing actors out of 100k. Sure I guess it could
be a bot that sequentially tried them all, but this is not likely since we
have SEO style URLs and so we re-map an actor name back to the ID. So the
bot would have to try NAMEs not IDs. This means we must have some link
somewhere that points to this. Same with the common foreach() warnings
below. Yeah, the array being passed is empty/null. Sure I can check the
array before doing the foreach() or even @foreach() but that doesn't tell me
the root cause. What video are they trying to access that has no scenes or
invalid actors?

We do NOT have apache logging turned on as we get 30,000 hits per second and
it would be too expensive. I only care about PHP errors like this. And the
apache error log (which we do have enabled) doesn't have useful info related
to these kinds of issues as they're really not apache's problem. That log
only deals with missing files/images/pages/etc.

[28-Feb-2012 13:43:19 UTC] PHP Fatal error:  Uncaught exception 
'ObjectNotFound' with message 'There is no such object Actor [2206].' in 
/home/SHARED/classes/base.class.php:103
Stack trace:
#0 /home/SHARED/classes/actor.class.php(61): Base->load_from_sql()
#1 /home/m.videosz.com/browse_scenes.php(89): Actor->__construct(2206)
#2 {main}
   thrown in /home/SHARED/classes/base.class.php on line 103

[28-Feb-2012 10:54:01 UTC] PHP Warning:  Invalid argument supplied for 
foreach() in /home/m.dev.com/scene.php on line 138

[28-Feb-2012 07:22:50 UTC] PHP Warning:  Invalid argument supplied for 
foreach() in /home/SHARED/classes/scene.class.php on line 423



--- End Message ---
--- Begin Message ---
On Tue, Feb 28, 2012 at 6:14 PM, Daevid Vincent <dae...@daevid.com> wrote:

> My question is, is there a way to enable some PHP configuration that would
> output more verbose information, such as a backtrace or the URL attempted?
>
> In our PHP error log, we have the usual semi-useful information. However
> this is only a partial story as it's hard to re-create the URL that caused
> the error. In the first Actor example, yeah actor_id 2206 doesn't exist and
> so now I have put a try/catch on all pages that have "new Actor($actor_id)"
> but it doesn't tell me WHY this is happening. How did someone get to this
> point? I doubt they just randomly picked '2206' which happens to be one of
> only a handful of actually missing actors out of 100k. Sure I guess it
> could
> be a bot that sequentially tried them all, but this is not likely since we
> have SEO style URLs and so we re-map an actor name back to the ID. So the
> bot would have to try NAMEs not IDs. This means we must have some link
> somewhere that points to this. Same with the common foreach() warnings
> below. Yeah, the array being passed is empty/null. Sure I can check the
> array before doing the foreach() or even @foreach() but that doesn't tell
> me
> the root cause. What video are they trying to access that has no scenes or
> invalid actors?
>
> We do NOT have apache logging turned on as we get 30,000 hits per second
> and
> it would be too expensive. I only care about PHP errors like this. And the
> apache error log (which we do have enabled) doesn't have useful info
> related
> to these kinds of issues as they're really not apache's problem. That log
> only deals with missing files/images/pages/etc.
>
> [28-Feb-2012 13:43:19 UTC] PHP Fatal error:  Uncaught exception
> 'ObjectNotFound' with message 'There is no such object Actor [2206].' in
> /home/SHARED/classes/base.class.php:103
> Stack trace:
> #0 /home/SHARED/classes/actor.class.php(61): Base->load_from_sql()
> #1 /home/m.videosz.com/browse_scenes.php(89): Actor->__construct(2206)
> #2 {main}
>   thrown in /home/SHARED/classes/base.class.php on line 103
>
> [28-Feb-2012 10:54:01 UTC] PHP Warning:  Invalid argument supplied for
> foreach() in /home/m.dev.com/scene.php on line 138
>
> [28-Feb-2012 07:22:50 UTC] PHP Warning:  Invalid argument supplied for
> foreach() in /home/SHARED/classes/scene.class.php on line 423
>
>
>
> --
> PHP General Mailing List (http://www.php.net/)
> To unsubscribe, visit: http://www.php.net/unsub.php
>
>
I tend to set up a custom error handler that throws exceptions
(set_error_handler()), then set up an exception handler
(set_exception_handler()) that logs the backtrace (or saves it to a db)
available using debug_backtrace().

Adam

-- 
Nephtali:  A simple, flexible, fast, and security-focused PHP framework
http://nephtaliproject.com

--- End Message ---
--- Begin Message ---
On 28 Feb 2012, at 23:14, Daevid Vincent wrote:

> My question is, is there a way to enable some PHP configuration that would
> output more verbose information, such as a backtrace or the URL attempted?
> 
> In our PHP error log, we have the usual semi-useful information. However
> this is only a partial story as it's hard to re-create the URL that caused
> the error. In the first Actor example, yeah actor_id 2206 doesn't exist and
> so now I have put a try/catch on all pages that have "new Actor($actor_id)"
> but it doesn't tell me WHY this is happening. How did someone get to this
> point? I doubt they just randomly picked '2206' which happens to be one of
> only a handful of actually missing actors out of 100k. Sure I guess it could
> be a bot that sequentially tried them all, but this is not likely since we
> have SEO style URLs and so we re-map an actor name back to the ID. So the
> bot would have to try NAMEs not IDs. This means we must have some link
> somewhere that points to this. Same with the common foreach() warnings
> below. Yeah, the array being passed is empty/null. Sure I can check the
> array before doing the foreach() or even @foreach() but that doesn't tell me
> the root cause. What video are they trying to access that has no scenes or
> invalid actors?
> 
> We do NOT have apache logging turned on as we get 30,000 hits per second and
> it would be too expensive. I only care about PHP errors like this. And the
> apache error log (which we do have enabled) doesn't have useful info related
> to these kinds of issues as they're really not apache's problem. That log
> only deals with missing files/images/pages/etc.
> 
> [28-Feb-2012 13:43:19 UTC] PHP Fatal error:  Uncaught exception 
> 'ObjectNotFound' with message 'There is no such object Actor [2206].' in 
> /home/SHARED/classes/base.class.php:103
> Stack trace:
> #0 /home/SHARED/classes/actor.class.php(61): Base->load_from_sql()
> #1 /home/m.videosz.com/browse_scenes.php(89): Actor->__construct(2206)
> #2 {main}
>   thrown in /home/SHARED/classes/base.class.php on line 103
> 
> [28-Feb-2012 10:54:01 UTC] PHP Warning:  Invalid argument supplied for 
> foreach() in /home/m.dev.com/scene.php on line 138
> 
> [28-Feb-2012 07:22:50 UTC] PHP Warning:  Invalid argument supplied for 
> foreach() in /home/SHARED/classes/scene.class.php on line 423

Seriously? Errors like this should not be getting anywhere near your production 
servers. This is especially true if you're really getting 30k hits/s.

For a commercial, zero-hassle solution I can't recommend http://newrelic.com/ 
highly enough. Simple installation followed by highly detailed reports with 
zero issues (so far). They do a free trial of all the pro features so you can 
see if it gets you what you need. And no, I don't work for them, I just think 
they've built a freakin' awesome product that's invaluable when diagnosing 
issues that only occur in production. I've never used it on a site with that 
level of traffic, and I'm sure it won't be a problem, but you may want to only 
deploy it to a fraction of your infrastructure.

If you want a homemade solution, the uncaught exceptions are easily dealt 
with... CATCH THEM, do something useful with them, and then die gracefully. 
Rocket science this ain't! See the set_exception_handler function for an easy 
way to set up a global function to catch uncaught exceptions if you don't have 
a limited number of entry points.

You can similarly catch the warnings using the set_error_handler function, tho 
be aware that this won't be triggered for fatal errors.

But seriously... a minimal level of structured testing would prevent issues 
like this being deployed to your production servers. Sure, instrument to help 
resolve these issues now, but if I were you I'd be putting a lot of effort into 
improving your development process. Contact me off-list if you'd like to talk 
about this in more detail.

-Stuart

-- 
Stuart Dallas
3ft9 Ltd
http://3ft9.com/

--- End Message ---
--- Begin Message ---
> -----Original Message-----
> From: Stuart Dallas [mailto:stu...@3ft9.com]
> 
> Seriously? Errors like this should not be getting anywhere near your
> production servers. This is especially true if you're really getting 30k
> hits/s.

Don't get me started. I joined here almost a year ago. They didn't even have
a RCS or Wiki at the time. Nothing was OOP. There were no PHPDoc or even
comments in the code. They used to make each site by copying an existing one
and modifying it (ie. no shared libraries or resources). I could go on and
on. Suffice it to say we've made HUGE leaps and bounds (thanks to me!), but
there is only 3 of us developers here and no official test person let alone
a test team.

It is what it is. I'm doing the best I can with the limited resources
available to me.

And let me tell you a little secret, when you get to that scale, you see all
kinds of errors you don't see on your VM or even with a test team. DB
connections go away. Funny things happen to memcache. Concurrency issues
arise. Web bots and search engines rape, pillage and ravage your site in
ways that make you feel dirty. So yeah, you do hit weird situations and
cases you can't possibly test for, but show up in error logs.
 
> For a commercial, zero-hassle solution I can't recommend
> http://newrelic.com/ highly enough. Simple installation followed by highly
> detailed reports with zero issues (so far). They do a free trial of all
the
> pro features so you can see if it gets you what you need. And no, I don't
> work for them, I just think they've built a freakin' awesome product
that's
> invaluable when diagnosing issues that only occur in production. I've
never
> used it on a site with that level of traffic, and I'm sure it won't be a
> problem, but you may want to only deploy it to a fraction of your
> infrastructure.

A quick look at that product seems interesting, but not what I really need.
We have a ton of monitoring solutions in place to get metrics and
performance data. I just need a good 'hook' to get details when errors
occur.

> If you want a homemade solution, the uncaught exceptions are easily dealt
> with... CATCH THEM, do something useful with them, and then die
gracefully.
> Rocket science this ain't!

Thanks captain obvious. :)

I can do that (and did do that), but again, at these scales, all the
text-book code you think you know starts to go out the window. Frameworks
break down. RDBMS topple over. You have to write things creatively, leanly
(and sometimes error on the side of 'assume something is there' rather than
'assume the worst' or your code spends too much time checking the edge
cases). Hit it and quit it! Get in. Get out. I can't put try/catch around
everything everywhere, it's just not efficient or practical. Even the SQL
queries we write are 'wide' and we pull in as much logical stuff as we can
in one DB call, get it into a memcache slab and then pull it out of there
over and over, rather than surgical queries to get small chunks of data
which would murder mySQL.

Part of the reason I took this job is exactly because of these challenges
and I've learned an incredible amount here (I've also had to wash the guilt
off of me some nights, as some code I've written goes against everything I
was taught and thought I knew for the past decade -- but it works and works
well -- it just FEELS wrong). We do a lot of things that would make my
college professors cringe. THAT is the difference between the REAL world and
the LAB. ;-)

> See the set_exception_handler function for an
> easy way to set up a global function to catch uncaught exceptions if you
> don't have a limited number of entry points.
> 
> You can similarly catch the warnings using the set_error_handler function,
> tho be aware that this won't be triggered for fatal errors.

And this is the meat of the solution. Thanks! I'll look into these handlers
and see if I can inject it into someplace useful. I have high hopes for this
now.

> But seriously... a minimal level of structured testing would prevent
issues
> like this being deployed to your production servers. Sure, instrument to
> help resolve these issues now, but if I were you I'd be putting a lot of
> effort into improving your development process. Contact me off-list if
you'd
> like to talk about this in more detail.

See above. I have begged for even a single dedicated tester. I have offered
to sacrifice the open req I had for a junior developer to get a tester. That
resulted in them taking away the req because "clearly I didn't need the
developer then" and "we can just test it ourselves". You're preaching to the
choir my friend. I've been doing this for 15+ years at various companies.
;-)

d.


--- End Message ---
--- Begin Message ---
On Tue, Feb 28, 2012 at 3:14 PM, Daevid Vincent <dae...@daevid.com> wrote:
> My question is, is there a way to enable some PHP configuration that would
> output more verbose information, such as a backtrace or the URL attempted?
>

Have you looked at log4php? [1] It's a log4j (Java based) logging
facility port to PHP.  IIRC for log4j, you can do various logging from
FINEST, FINER, FINE, INFO, WARNING, ERROR (?), SEVERE levels within
the application.  You can adjust levels as needed at run time.  You
may want to have a wrapper that will do back trace and record the
requested URL.  The log4* facility does rolling file logging, DB,
e-mail, syslog, etc.  (I've used the log4j and log4net before.)  Very
handy and flexible, IMO.

HTH,
Tommy


[1] http://logging.apache.org/

--- End Message ---
--- Begin Message ---
Hi, Daevid

What you could do to have it quick is to install the plugin xdebug.

Here you can (as described in the documentation linked here) enable to get
some extra information for a E_* message from php.
http://xdebug.org/docs/stack_trace

I would not do that on a live-system where you have 30k v/s without
changing the default configuration as there are several options that would
dramatically slow down the system.
But if you're configuring this properly I think you'll get the best
information without changing the php-code itself.

Bye
Simon

2012/2/29 Tommy Pham <tommy...@gmail.com>

> On Tue, Feb 28, 2012 at 3:14 PM, Daevid Vincent <dae...@daevid.com> wrote:
> > My question is, is there a way to enable some PHP configuration that
> would
> > output more verbose information, such as a backtrace or the URL
> attempted?
> >
>
> Have you looked at log4php? [1] It's a log4j (Java based) logging
> facility port to PHP.  IIRC for log4j, you can do various logging from
> FINEST, FINER, FINE, INFO, WARNING, ERROR (?), SEVERE levels within
> the application.  You can adjust levels as needed at run time.  You
> may want to have a wrapper that will do back trace and record the
> requested URL.  The log4* facility does rolling file logging, DB,
> e-mail, syslog, etc.  (I've used the log4j and log4net before.)  Very
> handy and flexible, IMO.
>
> HTH,
> Tommy
>
>
> [1] http://logging.apache.org/
>
> --
> PHP General Mailing List (http://www.php.net/)
> To unsubscribe, visit: http://www.php.net/unsub.php
>
>

--- End Message ---

Reply via email to